DAX Measure to count text items - must be a more elegant way

rodwhiteley

New Member
Joined
Jan 15, 2012
Messages
37
Sorry, DAX newbie here.

I have a biggish dataset (about 4 million rows) and in one column ( [Type] ) there are 42 unique text items ("Item 1","Item 2", ... "Item 42") that I'd like to summarise in a Pivot by date.

My clumsy solution as proof of concept was to create a series of 42 calculated columns in this table along the lines of:

IsItem1
=if([Type]="Item 1",1,0)

Then the next calculated column is
IsItem2
=if([Type]="Item 2",1,0)

etc.

I am then able to Pivot the sums of these columns against the dates, as well as summing them to make compound items (eg "IsItem1or2:=sum([IsItem1],[Item2]) but this seems clumsy and inelegant.
More importantly likely a waste of memory and is probably slowing down performance.

What is the better approach here, or should I just write the 42 extra columns?

Thanks in advance for any help
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the exciting world of Power Pivot. Indeed there are much better ways. Calculated Columns are what most novice Power Pivot users (that come from Excel) try to use, but it is normally the wrong approach. Power Pivot is very different to Excel, but you can learn it. I suggest you buy a good book - this will lay the foundations for you and get you off to a good start. As it turns out I have written a very good book for people just like you. Read the reviews on Amazon and then decided if it is right for you. Learn to Write DAX - the book for all Excel users

To answer your questions
Don't create any columns.
Insert a new pivot table
put your column [Type] on Rows in the pivot
Write the following Measure (Calculated Field in Excel 2013) and add it to values.
Count = countrows(myTable)
 
Upvote 0
Hi
If I understand you right, you want to have a measure which calculates count items for "Item 1", "Item 2" only. For that you can use CALCULATE function.
Let your table name is myTable
Code:
IsItem1or2:=CALCULATE(COUNTROWS('myTable'), 'myTable'[Type]="Item 1" || 'myTable'[Type]="Item 2")
Regards,
 
Upvote 0
Not quite, I need to know the numbers of each individual item (there are 42 unique values), but I think your solution will work by me writing 42 measures (IsItem1; IsItem2; IsItem3:...) and then any combinatorial measures as you suggest.
Thanks for the input, I'll check and see if this approach works.
Rod
 
Upvote 0
You don't need to write 42 measures. Just follow the instructions I provided above - only 1 measure required to deliver the total of each of the 42 items in your list.
 
Upvote 0
You don't need to write 42 measures. Just follow the instructions I provided above - only 1 measure required to deliver the total of each of the 42 items in your list.
Thanks Matt, that makes sense, looking up your book now.
If I need to make combinatorial sums (eg what's the total of, say, items 1,3,4, and 8) I'd then write individual measures there?
It seems OR can only handle 2 arguments. What's the best approach to summing such arbitrary individual amounts? (I guess a slicer might work...)
 
Upvote 0
If you need these combinations, you definitely can write a hard coded measure for this. But if these are common groupings, it is easier/better to create a lookup table (you actually should create a lookup table anyway).

So you could have a lookup table like this.

Code:
Type      Group
Item 1    A
Item 2    B
Item 3    A
Item 4    C
Item 5    B
etc

Then instead of putting the Type in your pivot table, you put the Group instead. Using this approach makes it much easier to maintain the groupings and you can reuse the one single measure.

There is no one measure, it depends on your needs.
 
Upvote 0

Forum statistics

Threads
1,224,151
Messages
6,176,713
Members
452,740
Latest member
MrCY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top