Tiny Lizard

Power Pivot Consulting and Training

  • About
  • Blog
  • Power Pivot
  • Training
  • Contact

DAX Table Query Example

September 4, 2014 By Scott Senkeresty 1 Comment

As I referenced in my previous blog entry, we should take a look at DAX Table Queries.  In the world I live in, which is really focused on Power Pivot, you really don’t see these queries too often.  It probably doesn’t help that it’s a rather “hidden” feature in Excel.  I get the feeling it is more common to write these queries when doing work against a Tabular server, but that’s typically isn’t my space.

Still, I think there are some interesting reasons to use them in Excel… if nothing else just because it is kinda neat Winking smile   In the previous article, this came up in the context of “detailimage reports”.  Those reports that have lots of cells can be a performance problem, for the simple fact that each and every cell in the pivot table is evaluated completely independently.  This gives us a way to generate all the data with just one query.

Here we see a pivot table with Category, Subcategory, CalendarYear and MonthName on rows,  Countries on columns, and Sales LTD (Lifetime to Date) for our values.

imageLet’s work through the magic to soooooorrrta get the same thing through a DAX query.  Oh, now would probably be a good time to mention this works in Excel 2013 only.  (1) On the Data Tab, (2) Click “Existing Connections”, then the (3) Tables tab on the popup.  Then here is the where I always get confused Smile   I always want to click on the top entry, but what you actually need to do is (4) double-click any table from your model.   I wouldn’t imagepick the Sales table Smile   It is going to bring in all the data from that table.

On the Import Data dialog that shows up, as long as you see “Table” enabled and selected, you are happy and you can just click “OK”.   When I mess up… it is not enabled, nor selected.  Then I am sad.  Don’t be sad.

At this point, you will see a table of (say) your Calendar data.  Which, of course, isn’t what you want.  imageSo… you right-click on any cell in the table, and find this top-secret option to “Edit Dax…” which will bring up a dialog where you can edit the text of your query.

Our goal is to replicate the pivot table from above, which I will be honest… I have no idea close we can get to that goal.  I’m only qualified to pull the data out in some shape.  Below right you can see the dialog where you can edit the DAX, and the query I have used.  It is a straight-forward call to Summarize.  image

We pass the Sales table into the first param of Summarize, then all the columns to use for our dimensions.  We are allowed to use these columns from other tables, for the simple reason that they are related.  If we tried to use a column from a table other than one of our lookup tables, we would get an error.

And if we can ignore the margins that are too large on my blog, and the font that is too big, and the screen shots that take up like all the space that I want to use for writing… you will see the final result. Smile (Yes, I am working on that problem – iimagef you are reading this in the future, and it makes no sense, hurray!  I was successful!)

So, ignoring the collapsible hierarchy fields in the original pivot table… this still looks quite a bit different, for the simple fact that the original pivot table had the Country field across columns, instead of on rows like we have here.  I suppose I could write some Country specific measures [Sales YTD – Canada] but that certainly feels completely like cheating.  If there is some magic way to add the Years across columns of the table, somebody please tell me Smile

Aside from the different shape… the other sadness here is the lack of slicers.  Not that we had them in our initial pivot table, but we could.  Here we can’t.  Well, that isn’t totally true.  Clearly, you would have to be a sick freak to use a disconnected slicer and VBA to fake it, but… well, Chris Webb is that kind of sick freak.

I will close with something I learned from  Marco Russo and Alberto Ferrari.  I want to say it was in one of their books, but I have read so many books and articles, and watched so many videos, that I can’t keep them straight anymore.

When all is said and done, the output of our DAX Query is a table in Excel.  You know what you can do with tables in Excel?   Of course you do.  You can use them in your Power Pivot model as a Linked Table.  And that will completely work with these crazy tables made via DAX.   You would be taking data out of your model, running the query on it to create a new table, then linking that new table back into the power pivot model.   It’s seems crazy, but it totally works.

In our example here, we have taken daily data and converted into Monthly data (by category, subcategory and country).  We can then bring that back to Power Pivot to do some month-granularity calculations (say, against monthly budget) and kick-butt performance levels since we have so many fewer rows.     It is neat, it is crazy, it is just what you expect out of those wacky Italians.

ALL() vs ALLSELECTED()
Power Pivot Performance Gotchas

Filed Under: Power Pivot

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 280 other subscribers

Comments

  1. Josh Blackman says

    May 22, 2017 at 7:25 pm

    This is amazing, this could really change the game on how I develop models. Thanks for sharing the information.

    1. Do you have any deeper information on how they tables would refresh once they are reloaded into the data model? Would they refresh after Power Query is complete like the behavior of a calculated column?

    2. In general, can this be used as a substitute for calculated tables in Excel Power Pivot models?

    3. Can this be used to simulate the behavior of an incremental data load within a power pivot model?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Blog Posts

  • The streak is alive! August 10, 2017
  • DAX KEEPFILTERS Plus Bonus Complaining! July 20, 2017
  • VAR: The best thing to happen to DAX since CALCULATE() July 11, 2017
  • Review: Analyzing Data with Power BI and Power Pivot for Excel June 20, 2017
  • Power BI Date Table June 2, 2017

Follow on Twitter

My Tweets

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 280 other subscribers

Copyright © 2022 Tiny Lizard