Forum Discussion
Dynamic Table in DAX expression
Hello All,
I have the below dataset of products and store information. I would like to have a simple DAX Expression which returns Product and total sales.
Main Dataset:
| City | Product A | Product B | Product C | Product D |
| New York | 2 | 3 | 5 | 6 |
| Florida | 4 | 5 | 4 | 5 |
| Toronto | 5 | 2 | 6 | 1 |
| Boston | 7 | 1 | 4 | 3 |
The expected result in DAX expression:
| Product Type | Total Sales |
| Product A | 18 |
| Product B | 11 |
| Product C | 19 |
| Product D | 15 |
I don't mind writing hardcoded values for the Product. Basically, I would like to write some DAX expression like
Create new table
> Add row ( "Product A", Sum(ProductA),
> Add row ( "Product B", Sum(ProductB)
Return Table.
I don't want to create a separate calculated table as it doesn't sync with the main dataset on using a filter so looking to created Dax expression which will return the table and also remains in sync with the main dataset so whenever the filters are changed my values in the Dax table also changes.
Thanks in advance for the help.
9 Replies
- AnkitKukrejaSuper User
Hi Anonymous
This seems quite straight forward, please go into query editor and select your product columns and unpivot them,
you'll get a desired result and for this you might not require dax as well, just drag the columns and it would sum up your data or you can use measure as well.Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904- AnonymousNot applicable
Hi Ankit,
Thanks for replying. In my case the unpivot option won't work so looking for a measure option here which returns the product and it's total. Will be great if you can tell me the DAX expression for the same.
Thanks,
Jay
- AnkitKukrejaSuper User
Hi
Not sure what you were looking for exactly, but unpivot would be the best approach considering the scenario you have shared here. But, if you've some other requirements, then please use below dax for getting the same results.Sales by Product =
UNION(
SELECTCOLUMNS( 'Table (2)' , "Product A" , "Product A" , "Sales" , 'Table (2)'[Product A] ),
SELECTCOLUMNS( 'Table (2)' , "Product B" , "Product B" , "Sales" , 'Table (2)'[Product B] ),
SELECTCOLUMNS( 'Table (2)' , "Product C" , "Product C" , "Sales" , 'Table (2)'[Product C] ),
SELECTCOLUMNS( 'Table (2)' , "Product D" , "Product D" , "Sales" , 'Table (2)'[Product D] )
)
Thanks,
Ankit Kukreja
www.linkedin.com/in/ankit-kukreja1904