Forum Discussion
Max Value based upon two columns with distant filter
- 1 year ago
Hello CScottP
Thank you for providing the dataset and the expected result table.You can create the following DAX measure to calculate number of items:
Item Count = VAR _maxModOrd = MAX ( 'Table'[MOD_ORDER] ) VAR _actionValues = CALCULATE ( MAX ( 'Table'[ACTION] ), 'Table'[MOD_ORDER] = _maxModOrd ) VAR _items = CALCULATE ( SUM ( 'Table'[ITEMS] ), 'Table'[MOD_ORDER] = _maxModOrd ) VAR _trueItems = IF ( _actionValues = "DELETE", 0, _items ) // For Total Calculation VAR _summarizedTable = SUMMARIZE ( 'Table', 'Table'[TYPE], "max_mod_order", CALCULATE ( MAX ( 'Table'[MOD_ORDER] ) ) ) VAR _extendedTotalTable = FILTER ( ADDCOLUMNS ( _summarizedTable, "action", LOOKUPVALUE ( 'Table'[ACTION], 'Table'[TYPE], [TYPE], 'Table'[MOD_ORDER], [max_mod_order] ), "items", LOOKUPVALUE ( 'Table'[ITEMS], 'Table'[TYPE], [TYPE], 'Table'[MOD_ORDER], [max_mod_order] ) ), [action] <> "DELETE" ) VAR _totalItems = SUMX ( _extendedTotalTable, [items] ) RETURN IF ( ISINSCOPE ( 'Table'[TYPE] ), _trueItems, _totalItems )Here is the screenshot of the result I got after applyint this DAX measure:
Here is the screenshot of the DAX measure:
I am also attaching the working Power BI file that I have used to solve your query, in case you need it.
Best Regards,
Udit
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍
🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
✨ Visit My Linktree: LinkTreePround to be a Super User
Hello CScottP
Thank you for providing the dataset and the expected result table.
You can create the following DAX measure to calculate number of items:
Item Count =
VAR _maxModOrd = MAX ( 'Table'[MOD_ORDER] )
VAR _actionValues = CALCULATE ( MAX ( 'Table'[ACTION] ), 'Table'[MOD_ORDER] = _maxModOrd )
VAR _items = CALCULATE ( SUM ( 'Table'[ITEMS] ), 'Table'[MOD_ORDER] = _maxModOrd )
VAR _trueItems = IF ( _actionValues = "DELETE", 0, _items )
// For Total Calculation
VAR _summarizedTable =
SUMMARIZE (
'Table',
'Table'[TYPE],
"max_mod_order", CALCULATE ( MAX ( 'Table'[MOD_ORDER] ) )
)
VAR _extendedTotalTable =
FILTER (
ADDCOLUMNS (
_summarizedTable,
"action",
LOOKUPVALUE (
'Table'[ACTION],
'Table'[TYPE], [TYPE],
'Table'[MOD_ORDER], [max_mod_order]
),
"items",
LOOKUPVALUE (
'Table'[ITEMS],
'Table'[TYPE], [TYPE],
'Table'[MOD_ORDER], [max_mod_order]
)
),
[action] <> "DELETE"
)
VAR _totalItems =
SUMX ( _extendedTotalTable, [items] )
RETURN
IF ( ISINSCOPE ( 'Table'[TYPE] ), _trueItems, _totalItems )
Here is the screenshot of the result I got after applyint this DAX measure:
Here is the screenshot of the DAX measure:
I am also attaching the working Power BI file that I have used to solve your query, in case you need it.
Best Regards,
Udit
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍
🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
✨ Visit My Linktree: LinkTree
Pround to be a Super User
- CScottP1 year agoRegular Visitor
Sir,
Perfect! Thank you for your time and effort, greatly apprecated!
Scott
- CScottP1 year agoRegular Visitor
When I apply to larger data volume I'm getting the following, I did pair it off to same column names and checked column types as well.
Mdx Script (Model) (47,37) Clulation error in measue with card & Multi card in your example.
Measure showed - The function SUM can not work with values of type string.
Thanks in advance!
- quantumudit1 year agoSuper User
Hello CScottP
I suspect the issue lies within the data model. Sharing a sample, anonymized Power BI file would be extremely helpful for me to diagnose the problem.
DAX formulas generally perform consistently regardless of data size, provided the data types and other relevant factors are identical. Since it's not working as expected, I'll need to examine the Power BI file and other details to understand why.
If you prefer not to share the anonymized Power BI file publicly, please feel free to send it to me directly through message.
Thanks,
Udit