Forum Discussion
Calculate Firstnonblank based on the priority DAX
- 2 years ago
output
uk pallet ( result ) = var itm = Table_4[Item] var uk_pallet = Table_4[UK Pallet] var us_pallet = Table_4[US Pallet] var euro_pallet = Table_4[EURO Pallet] var ds = ADDCOLUMNS( filter( ALLNOBLANKROW(Table_4), Table_4[Item] = itm ), "@X", CALCULATE(DISTINCTCOUNT(Table_4[Mode]),ALLEXCEPT(Table_4,Table_4[Item])), -- check if we have more than 1 mode per item "@C" , CALCULATE(COUNT(Table_4[Mode]), ALLEXCEPT(Table_4,Table_4[Item],Table_4[Mode])) -- check nb item-mode existence ) var suk = SUMX(ds,Table_4[UK Pallet]) var sus = SUMX(ds,Table_4[US Pallet]) var seuro = SUMX(ds,Table_4[EURO Pallet]) var suk_same = suk/countrows(ds) = uk_pallet var sus_same = sus/countrows(ds) = us_pallet var seuro_same = seuro/countrows(ds) = euro_pallet var C= MAXX( ds, [@C] ) --check if has different mode -- if check returns 0 , then we hav only 1 mode per this item. var X = MAXX( ds ,[@X]) var air_data = SELECTCOLUMNS( FILTER( ALLNOBLANKROW(Table_4), Table_4[Item] = itm && Table_4[Mode] = "Air" ), Table_4[UK Pallet] ) var res = SWITCH( TRUE(), X > 1 && C = 1 && suk_same = TRUE() , "" &suk / countrows(ds)& "" , X > 1 && C = 1 && suk_same = False() , "" & air_data & "", X = 1 && C > 1 && suk_same = False() , "X", X > 1 && suk_same = False() , "X", x>1 && c > 1 && suk_same = TRUE() ,"" & air_data & "" ) return res -- CONCATENATEX(ds,[@X] & "-" & [@C] ,",")If my answer helped sort things out for you, i would appreciate a thumbs up 👍 and mark it as the solution ✅
It makes a difference and might help someone else too. Thanks for spreading the good vibes! 🤠 - 2 years ago
Hy,
I was able to get the desired result using this calculated dax column
UK Pallet Result = VAR currentItem = 'RESULT'[Item] VAR uniqueValues = CALCULATETABLE( DISTINCT('DATA'[UK Pallet]), ALL('DATA'), 'DATA'[Item] = currentItem ) VAR countOfUniqueValues = COUNTROWS(uniqueValues) VAR airValue = CALCULATE( MIN('DATA'[UK Pallet]), ALL('DATA'), 'DATA'[Item] = currentItem, 'DATA'[Mode] = "Air" ) VAR trainValue = CALCULATE( MIN('DATA'[UK Pallet]), ALL('DATA'), 'DATA'[Item] = currentItem, 'DATA'[Mode] = "Train" ) VAR airValueCount = CALCULATE( COUNTROWS('DATA'), ALL('DATA'), 'DATA'[Item] = currentItem, 'DATA'[Mode] = "Air" ) VAR trainValueCount = CALCULATE( COUNTROWS('DATA'), ALL('DATA'), 'DATA'[Item] = currentItem, 'DATA'[Mode] = "Train" ) VAR resultValue = IF( countOfUniqueValues = 1, FORMAT(MINX(uniqueValues, [UK Pallet]), "General Number"), IF( countOfUniqueValues > 1 && (airValueCount = 1 || trainValueCount = 1), IF( airValueCount = 1, FORMAT(airValue, "General Number"), IF( trainValueCount = 1, FORMAT(trainValue, "General Number"), "X" ) ), "X" ) ) RETURN resultValuePBIX file link: Calculate Firstnonblanks with priprity-0902.pbix
Here is the output:
Try to add a new DAX calculated column in your DATA table as...
p.s ( I used a -1 values instead of 'x' to keep it a numeric data type )
UK Pallet Logic =
VAR currentItem = DATA[Item]
VAR itemRows = FILTER(ALL(DATA), DATA[Item] = currentItem)
VAR uniqueModes = DISTINCT(SELECTCOLUMNS(itemRows, "Mode", DATA[Mode]))
VAR rowCount = COUNTROWS(itemRows)
VAR airRowCount = COUNTROWS(FILTER(itemRows, DATA[Mode] = "Air"))
RETURN
IF(
rowCount = 1, // If there is only one row for the item
MAXX(itemRows, DATA[UK Pallet]), // Return the UK Pallet value for that row
IF(
airRowCount = 1, // If there is exactly one 'Air' mode
MAXX(FILTER(itemRows, DATA[Mode] = "Air"), DATA[UK Pallet]),
-1 // If none of the above conditions are met
)
)
- Saxon2022022 years agoHelper III
amustafa, Thanks for your reply.
I am looking for UK pallet value into report table from data table based on the item and mode not within the table.
The result are incorrect as well.
Example: Item 132 expected value is 236 but your dax suggested - 1.
Thank you.