You probable need the ADDCOLUMNS() function, not quit sure though
Pergunta
I have the following DAX query for the Adventure Works DB:
evaluate
(
summarize
(
'Internet Sales',
'Product Category'[Product Category Name],
'Product Subcategory'[Product Subcategory Name],
'Product'[Product Name],
'Date'[Calendar Year],
"Total Sales Amount", sum('Internet Sales'[Sales Amount])
)
)
order by 'Product Category'[Product Category Name],
'Product Subcategory'[Product Subcategory Name],
'Product'[Product Name]
This returns the data in this format:
Accessories Bike Racks Hitch Rack - 4-Bike 2008 22920
Accessories Bike Racks Hitch Rack - 4-Bike 2007 16440
Accessories Bike Stands All-Purpose Bike Stand 2008 20670
I want to return it as this:
CATEGORY SUB CATEGORY PRODUCT 2007 2008 2009 2010
Accessories Bike Racks Hitch Rack - 4-Bike 22920 16440 22920 16440
Accessories Bike Stands All-Purpose Bike Stand 20850 20670 22920 16440
There should be a column for every year in the result set.
Is this possible? if so how?
Thanks
Solução 2
Outras dicas
Something like:
EVALUATE
ADDCOLUMNS(
SUMMARIZE(
'Internet Sales',
'Product Category'[Product Category Name],
'Product Subcategory'[Product Subcategory Name],
'Product'[Product Name]
),
"Total Sales Amount", CALCULATE(SUM('Internet Sales'[Sales Amount])),
"2007", CALCULATE(SUM('Internet Sales'[Sales Amount]), 'Date'[Calendar Year] = 2007)
"2008", CALCULATE(SUM('Internet Sales'[Sales Amount]), 'Date'[Calendar Year] = 2008)
"2009", CALCULATE(SUM('Internet Sales'[Sales Amount]), 'Date'[Calendar Year] = 2009)
)
ORDER BY 'Product Category'[Product Category Name],
'Product Subcategory'[Product Subcategory Name],
'Product'[Product Name]
DAX is not as friendly to 'hack away at a solution' as other languages like SQL. There does not exist a pivot function in DAX and the only way to pivot items, is manually.
There are some creative solutions but they all revolve around a manual solution i.e. supplying the columns and logic you require. The consequence of this is that every time a year is added you will have to manually add it to your query.
If there is a better solution, I would be glad to hear, I am still looking myself.
BTW you do not need the brackets for the evaluate function so I personally do not bother using them but to each his own.
It's not possible, as that's not how it's supposed to work. Or rather, it works somewhat automatically when you move from DAX Studio to the actual measure.
Assuming you're using the years as row headers in your pivot table, they will automatically impose a filter on the summarize result in your measure (actually the filter already applies to summarize's input table).
Therefore only the results for the respective year will be in your summarize table (i.e. you might just as well remove the group by column for year).