Summarize values based on two columns DAX - powerbi

I'm new to DAX and don't get how it works (I'm studying its context transitions and so on). What I would like to do is to calculate the marked column without duplicating the stock. I mean, it should only sum stock once by PRODUCT. In this case, instead of 480, it should be 240 in every line (including the one that is 0, not sharing product).
This is my measure:
REF STOCK*LINEA :=
CALCULATE (
SUM ( Production[REF_STOCK] );
FILTER (
ALLEXCEPT (Production; Production[SIAC] );
Production[PENDING_UNITS] > 0
&& Production[SIAC 1] <> "SIAC"
)
)
EDIT: I think I made it.
This measure apparently works, but I'm not sure if it is correct for what I want to achieve. Will it work with every case?
REF STOCK*LINEA :=
CALCULATE (
SUMX (
SUMMARIZE ( Production; Production[SIAC]; Production[REF]; Production[Description] );
[Avg of REF_STOCK]
);
FILTER (
ALLEXCEPT ( Production; Production[SIAC] );
Production[PENDING_UNITS] > 0
&& Production[SIAC 1] <> "SIAC"
)
)
Please confirm it should work as expected. Thank you!

Related

DAX is not returning same value when using exact same statement, but with and without defining variables

Please refer to the syntax and table below. I'd like to use the variable, making it more efficient and readable of course, but it doesn't seem to work for me. It is the exact same statement, however I'm (not) defining the variable in the measure "testDAX" ("testDAX2").
I'm looking for the output value being 137, as given by testDAX. The 830 is simply the count of all rows in the table, without applying the filter of ProdCount = 1.
Just added the _tableD6 code. Then I copied the table in the DAX code exactly as is.
FYI: Curbal does the same thing in this video, starting at 3:00, but slightly less complex (without variables). https://www.youtube.com/watch?v=XeV0ndga5kg&t=218s
Thanks in advance!
YK
Code:
testDAX =
CALCULATE (
COUNTROWS (
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) )
),
FILTER (
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) ),
[ProdCount] = 1
)
)
and
testDAX2 =
VAR _tableD6 =
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) )
RETURN
CALCULATE ( COUNTROWS ( _tableD6 ), FILTER ( _tableD6, [ProdCount] = 1 ) )

Count if <= row by row calculation

I am trying to count the number of results that are <= 25%.
See bellow for example data
I am trying to create a measure that counts if "Pallet Utilization" is <= 25%.
"Pallet Utilization" is not a column within the data, it would need to be calculated within the measure.
From my understanding, i need to ask the measure to calculate Row by Row?
Bellow is my attempt at doing this however it is returning a count of all rows
AC_Less25 =
CALCULATE (
COUNTAX (
Chilterns_STORAGE,
DIVIDE (
DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX]
) <= 0.25
)
)
Not very experienced with DAX so any help appreciated.
Thanks
So you could use COUNTX, but you can also just use COUNT and add a calculated column.
Add a new Calculated Column:
Pallet Utilization =
DIVIDE (
DIVIDE ( Chilterns_STORAGE[NO_CASES], Cilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX],
BLANK ()
)
And then add the new measure:
AC_Less25 =
CALCULATE (
COUNT ( Chilterns_STORAGE[Pallet Utilization] ),
FILTER ( Chilterns_STORAGE, Chilterns_STORAGE[Pallet Utilization] <= .25 )
)
EDIT:
If you're dead-set on using COUNTX, something like this would help. In a COUNTX ( or any 'X' measure for that matter ), you define the table you want to iterate over, then provide what it is counting/summing/averaging as the second parameter. So conditions are placed on the table like so:
AC_Less25 = COUNTX(
FILTER(Chilterns_STORAGE,
DIVIDE(
DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX]) <= .25),
Chilterns_STORAGE[NO_PALLETS])
Please note that I'm not sure my Pallet Utilization is correct because I'm not getting the same numbers as you are in your OP... But the screenshot speaks for itself and the CountX above will still do what you want it to do, provided you tweak the conditions in the first parameter of the CountX: DIVIDE(DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ), Chilterns_STORAGE[POU_MAX]) <= .25))

Is there Dax code to take the difference between 2 rows from a summarized table

I need to be able to get the difference between 2 successive rows in a Summarized table, by rank, so that I can then get the average of the differences of each row. And I can't create a new table as I need this DAX query to be filterable
I've been able to get this far, but do not know how to add a difference column that will show the DSOValue difference between rows 1-2, 2-3, 3-4 ...
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[Date],
"DSOValue", DIVIDE ( SUM ( 'Table1'[AR] ) * 91.5, SUM ( 'Table1'[Sales] ), 0 )
),
"Rank", RANKX (
Table1,
CALCULATE (
COUNTROWS ( Table1),
FILTER ( Table1, Table1[Date] <= EARLIER ( Table1[Date] ) )
),,ASC,DENSE)
)
I've tried embedding this code within another ADDCOLUMNS function, but it won't let me CALCULATE and FILTER on my created columns (DSOValue and RANK)
you can use the following:
Diff = 'Table1'[ DSOValue ] - LOOKUPVALUE('Table1'[ DSOValue ]; 'Table1'[Date ];CALCULATE( MAX('Table1'[Date ]);FILTER('Table1';'Table1'[Date ]<EARLIER('Table1'[Date ]))))
Note: You cannot use <= here, it will pick its own date and all will be null. It would also be easier to add an index column, when you have this you can use Lookupvalue with Index -1

filtering measures based on two columns in power bi dax

I want to use a measure and filter the result based on the columns:
My measure is :
TotalProductionCon =
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] )
and I want it to summarize only when column année = column year.
I tried CALCULATE and FILTER;
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] );
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
but it generates an error that the columns contain much value and I need to use aggregation.
Can you help me?
The problem with your formula is that you limited ALL function to only one column (Annee), and as a result FILTER does not "see" the other column it needs.
To fix that, change your formula as follows:
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL (
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année];
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
);
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
I am assuming here that your choice of ALL function is appropriate; otherwise you might need to use a different technique such as SUMMARIZE function.

DAX using IF and AND functions

I'm trying to create a trending table with a value and a forecast. The forecast needs to start from the current month going forward. I am using this dax function:
Spend Forecast =
IF (
OR (
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Split] = "Spend Actual" )
),
1000000
)
< 1,
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Ind_Monthend] = "x" )
),
1000000
)
< 1
),
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Split] = "Spend Forecast" )
),
1000000
),
""
)
The formula is calculating to check if these two conditions are met:
if there's no value then populate the forecast or if the ind monthend = 'x' then it should populate, if those two conditions are not met then it should leave it blank.
There are no syntax errors on the query but i am getting this error:
The True/False expression does not specify a column. Each True/False
expressions used as a table filter expression must refer to exactly
one column
Where did I go wrong?
It's very hard to comprehend such long formulas. It's the best practice to break them down into multiple measures. For example, your code can be re-writen as follows:
Create base measure:
Total Spend = SUM ( refv_Spend_Cap[Spend_2019] ) / 1000000
Now re-use the base measure to create 3 conditional measures. No need to use FILTER here:
Spend Actual = CALCULATE ( [Total Spend], refv_Spend_Cap[Split] = "Spend Actual" )
Spend X = CALCULATE ( [Total Spend], refv_Spend_Cap[Ind_Monthend] = "x" )
Spend Forecast = CALCULATE ( [Total Spend], refv_Spend_Cap[Split] = "Spend Forecast" )
Then the final result is simply:
Forecast = IF ( [Spend Actual] < 1 || [Spend X] < 1, [Spend Forecast], "")
It's much easire to understand what's happening, and easier to debug. You will also gain perfomance bonus because (usually) re-used measures are cached and calculated only once.
Try this code, if it still gives you problems, describe the new error and I'll help you fix it.
BTW, there is a popular free tool to format your DAX code:
Dax Formatter