DAX: Removing a filter for a measure in the SUMMARIZECOLUMNS query - dax

I'm struggling to create a DAX query to get the weekly sales amount and total sales amount (sum of all weeks together) on a same row. I'm filtering on item and year.
Example of a query:
EVALUATE
SUMMARIZECOLUMNS(
Artikel[ART Artikel],
Datum[Jahr-Woche],
FILTER (
Artikel,
Artikel[ART ArtikelNr]="222834"
),
FILTER (
'Datum',
Datum[Jahr]="2022"
),
"Sales Quantity ", [VK Menge],
"Sales Quantity Total", CALCULATE([VK Menge], ALL(Datum[Jahr-Woche]) )
)
The Sales Quantity Total doesn't return the yearly total - it returns the same value as Sales Quantity. It seems that the ALL() function is not removing the filter from the column Datum[Jahr-Woche]):
I've tried using REMOVEFILTERS(), etc. to no success.
What am I doing wrong?
Note 1:
When I remove the filtering on Datum[Jahr] from the query, the ALL() function starts working - the Sales Quantity Total returns the total sum. However - I need to filter on the year in the query... This behaviour is very confusing. :(
EVALUATE
SUMMARIZECOLUMNS(
Artikel[ART Artikel],
Datum[Jahr-Woche],
FILTER (
Artikel,
Artikel[ART ArtikelNr]="222834"
),
"Sales Quantity ", [VK Menge],
"Sales Quantity Total", CALCULATE([VK Menge], ALL(Datum[Jahr-Woche]) )
)

Please try this code and let me know If It works for you! Calculate evaluates external filters first, then internal filters!
YourMeasure =
SUMMARIZECOLUMNS (
Artikel[ART Artikel],
Datum[Jahr-Woche],
FILTER ( Artikel, Artikel[ART ArtikelNr] = "222834" ),
"Sales Quantity ", [VK Menge],
"Sales Quantity Total",
CALCULATE (
CALCULATE ( [VK Menge], Datum[Jahr] = "2022" ),
ALL ( Datum[Jahr-Woche] )
)
)
EXPLAIN Above Same Code:

Related

PowerBi - How to show multiple total columns in Matrix?

I have a slicer from which I can select weeks that I want to show on a matrix. Currently, the matrix only shows the total column at the end of all columns basically summing up all the values.
But I want to show multiple total column (One after each month). Like after the End Of Weeks of one month a total column and so on.
You cannot simply create a Calculated column or Measure to solve this problem. Actually, you can use some logic with Calculated column, but there is a more efficient way. To achieve the goal, you have to create Calculated table based on your date column. You can use that logic to create any custom label for your visuals.
Before we start diving into deep DAX here, let's create a calendar table.
Calendar =
VAR _minSalesDate = CALCULATE( MIN( Sales[Date] ), ALL( Sales ) )
VAR _maxSalesDate = CALCULATE( MAX( Sales[Date] ), ALL( Sales ) )
RETURN
ADDCOLUMNS(
CALENDAR( _minSalesDate, _maxSalesDate ),
"Month", MONTH( [Date] ),
"MonthYear", MONTH( [Date] ) & "-" & YEAR( [Date] )
)
Now, when we have the table with our dates and additional fields, we can start creating a solution for this particular case.
Create a new calculated table as follows:
CustomCategories =
UNION(
SELECTCOLUMNS(
SUMMARIZE(
'Calendar',
'Calendar'[Date],
'Calendar'[Sorter]
),
"Key", 'Calendar'[Date],
"Label", 'Calendar'[Date],
"LabelSorter", 'Calendar'[Sorter]
),
ADDCOLUMNS(
SELECTCOLUMNS(
SUMMARIZE(
'Calendar',
'Calendar'[Date],
'Calendar'[MonthYear],
'Calendar'[Sorter]
),
"Key", 'Calendar'[Date],
"Label", 'Calendar'[MonthYear]
),
"LabelSorter", CALCULATE( MAX( 'Calendar'[Sorter] ), ALLEXCEPT( 'Calendar', 'Calendar'[MonthYear] ) ) + 0.5
),
SELECTCOLUMNS(
ADDCOLUMNS(
SUMMARIZE(
'Calendar',
'Calendar'[Date],
'Calendar'[Sorter]
),
"LabelSorter", CALCULATE( MAX( 'Calendar'[Sorter] ), ALL( 'Calendar' ) ) + 1
),
"Key", 'Calendar'[Date],
"Label", "Total",
"LabelSorter", [LabelSorter]
)
)
Note that the last part of above DAX adding Total is optional.
Once the table has been created, go to the Data view, select CustomCategories table, select Label column and sort the column by LabelSorter (you will find an option on the ribbon). Then go back to your Model view and set a relationship between CustomCategories and your Fact table on Date column.
When you have done with all stuff above, switch back to the Report view. Remove the date column from your table visual and replace it with the newly created Label from CustomCategories table.
Now you should see the desired results.
Hope that helps!
Regards,
Arek

DAX Power Pivot: "= CONCATENATEX(CALCULATETABLE(" how to avoid duplicates?

I've seen similar posts before, but none of the solutions worked for me as my formula is complex and includes filters.
I'm trying to create a new column in a table, concatenating all matches from a column in a different table.(e.g. Order table concatenating all product names associated with a given order)
Full formula: =
CONCATENATEX (
CALCULATETABLE (
RoW_SIC_table,
FILTER (
RoW_SIC_table,
RoW_SIC_table[Excel Company ID] = Row_main[Excel Company ID]
),
FILTER ( RoW_SIC_table, NOT ( ISBLANK ( RoW_SIC_table[Broad] ) ) )
),
RoW_SIC_table[Broad],
", "
)
I want CONCATENATEX to only concatenate unique values, and I haven't made it work with either DISTINCT or VALUES. Can you help me?
How about like this?
Full formula: =
CONCATENATEX (
CALCULATETABLE (
DISTINCT ( RoW_SIC_table[Broad] ),
FILTER (
RoW_SIC_table,
RoW_SIC_table[Excel Company ID] = Row_main[Excel Company ID]
)
),
RoW_SIC_table[Broad],
", "
)
I used as reference this:
e.g. Order table concatenating all product names associated with a
given order
So what came to my mind is the following model:
SIC Table
Main Table
"Data Model"
Result
ProductsListedByOrderIFBroadISNOTBLANK=
VAR COrderID=SIC[OrderID]
VAR PIDWLineage=
SELECTCOLUMNS(
CALCULATETABLE(SIC,
ALL(SIC), SIC[OrderID]=COrderID, NOT(ISBLANK(SIC[Broad]))),
"ProductID", SIC[ProductID], "ProductName", RELATED(Main[ProductName]))
RETURN
CONCATENATEX(PIDWLineage, [ProductName], ",")

Distinct Count without using CALCULATE

I've come across this DAX measure:
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
CALCULATE( DISTINCTCOUNT( Sales[ProductKey] ) ) >= 2
)
)
I pretty much understand how it works - it iterates over Customer inside the FILTER function, then the row context created by this iterator is transitioned into a filter context so that it is able to get the number of distinct products from the Sales table.
I am wondering is it possible to re-write the measure without using CALCULATE ? I got as far as using RELATEDTABLE but then not sure how to extract the distinct ProductKeys from each related table:
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
RELATEDTABLE (Sales)
...
...
)
)
This is a possible implementation of the measure using RELATEDTABLE. But Context Transition still happens once per customer because RELATEDTABLE performs a context transition
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
VAR CustomerSales =
RELATEDTABLE( Sales )
RETURN
MAXX( CustomerSales, Sales[ProductKey] )
<> MINX( CustomerSales, Sales[ProductKey] )
)
)
This is another way to write a measure leveraging RELATEDTABLE, that could be modified to deal with a different number of distinct products
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
COUNTROWS( SUMMARIZE( RELATEDTABLE( Sales ), Sales[ProductKey] ) ) >= 2
)
)
This is another possible implementation, without CALCULATE and RELATEDTABLE.
But it scans the entire Sales table once per customer, so, even if it doesn't perform a context transition I'd expect it to be slower
# CustMultProds =
COUNTROWS(
FILTER(
Customer,
VAR SalesProducts =
SUMMARIZE( Sales, Sales[CustomerKey], Sales[ProductKey] )
RETURN
COUNTROWS(
FILTER( SalesProduct, Sales[CustomerKey] = Customer[CustomerKey] )
) >= 2
)
)

DAX - Advanced Product Grouping/Segmentation Question

I created an SSAS Tabular model using the AdventureWorksDW database.
I used the post below to help me build the report.
https://blog.gbrueckl.at/2014/02/applied-basket-analysis-in-power-pivot-using-dax/
Sold in same Order:=
CALCULATE (
COUNTROWS ( 'Internet Sales' ),
CALCULATETABLE (
SUMMARIZE (
'Internet Sales',
'Internet Sales'[SalesOrderNumber]
),
ALL ( 'Product' ) ,
USERELATIONSHIP( 'Internet Sales'[ProductKey],FilteredProduct[ProductKey])
)
)
I have validated that the results from the formula are correct. There are 1,381 orders with the Touring Tire Tube sold and shows me how many orders were sold with the other items (e.g. 170 out of the 1,381 orders also included product key 214 - Sport-100 Helmet, Red).
Here is where I'm having an issue. I would like to summarize my data by showing how many of the orders only included my filtered items vs. orders sold with other products. This has to be dynamic since users can select any products... The end result should look like this:
I'm new to DAX and have struggled with this for a few hours. Thanks for your help.
Here is the table relationship:
this DAX should work on the example dataset from my blog:
Orders with only the filtered products =
--VAR vFilteredProducts = VALUES('Filtered Product'[ProductKey])
VAR vFilteredProducts = FILTER('Filtered Product', [ProductKey] = 530 || [ProductKey] = 541)
VAR vCountFilteredProducts = COUNTROWS(vFilteredProducts)
VAR vSales = CALCULATETABLE('Internet Sales', -- get the Sales for the filtered Products
vFilteredProducts,
USERELATIONSHIP('Filtered Product'[ProductKey], 'Internet Sales'[ProductKey]),
ALL('Product'))
VAR vOrders = SUMMARIZE( -- Summarize the filtered product sales by Sales Order Number
vSales,
[Sales Order Number],
-- calucate the distinct filtered products in the filtered orders
"CountFilteredProductsInOrder", CALCULATE(DISTINCTCOUNT('Internet Sales'[ProductKey])),
-- calculate the all distinct products for the filtered orders
"CountTotalProductsInOrder", CALCULATE(DISTINCTCOUNT('Internet Sales'[ProductKey]),
ALLEXCEPT('Internet Sales', 'Internet Sales'[Sales Order Number]))
)
RETURN COUNTROWS(
FILTER(
vOrders,
-- the total product count has to match the filtered product count --> no other products except filtered ones in order
[CountFilteredProductsInOrder] = [CountTotalProductsInOrder]
)
)
To get the orders where also other products except the filtered ones were sold, imply change the last FILTER() from '=' to '<'

dax studio - Filtering Data

Using DAX studio, I'm trying to understand what data this filter function is pulling.
EVALUATE
FILTER ( 'TM Freight Charges',
'TM Freight Charges'[Related Order Type] = Fact_Table[Order Type])
However, I get the following error message:
error message image
ultimately, I'm trying to evaluate this particular filter formula
Evaluate
FILTER ('TM Freight Charges',
AND (
AND (
AND (
'TM Freight Charges'[Related Order Type] = [Order Type],
'TM Freight Charges'[Related Order Number] = [Order Number]
),
'TM Freight Charges'[Volume] = Fact_Table[Volume]
),
'TM Freight Charges'[Charge Type] = "BASE"
)
)
)
If this was a SQL problem, I would just do an INNER JOIN along with some WHERE statements, but in DAX Studio, I don't have a clue. Help?
If you specify an 'equal' on the filter, you should pass a value or a list or something. If you are doing that on a row context (for example adding a column to a table), the way you did it will work, but on building a table there's no context for that. Did you try something like:
EVALUATE
FILTER ( 'TM Freight Charges',
'TM Freight Charges'[Related Order Type] IN Fact_Table[Order Type])
or:
EVALUATE
FILTER ( 'TM Freight Charges',
'TM Freight Charges'[Related Order Type] IN VALUES ( Fact_Table[Order Type]) )
Cheers!

Resources