Calculating Max value of a measure in DAX - powerbi

I Need to calculate the Max value from a measure. Below is the scenario.
Expectation : Pareto%* column should return max value from RunningTotal* Measure which is 1982. Each row in Pareto%* should return 1982.
DAX which I tried:
DAX for Pareto%*
Pareto%* =
MAXX(ALL(EventDetail[TimelinessBreak]),[RunningTotal*])
Can anyone please suggest what is wrong with the above DAX.
[![enter image description here][2]][2]
DAX suggested by Marcus returning 3587177 which is incorrect.
Test34* =
CALCULATE (
[RunningTotal*] ,
ALL ( EventDetail )
)
Description for [RunningTotal*]
CALCULATE(
[ActualUpdates],
FILTER(
CALCULATETABLE(
SUMMARIZE(
'EventDetail',
'EventDetail'[TimelinessBreakSort],
'EventDetail'[TimelinessBreak]
),
ALLSELECTED('EventDetail')
),
ISONORAFTER(
'EventDetail'[TimelinessBreakSort], MAX('EventDetail'[TimelinessBreakSort]), DESC,
'EventDetail'[TimelinessBreak], MAX('EventDetail'[TimelinessBreak]), DESC
)
)
)
If I remove the timelinessbreak column from the table it is showing correct output with my DAX.

With the added information, I suggest:
Pareto%* =
MAXX (
SUMMARIZE (
ALL ( EventDetail ) ,
EventDetail[TimelinessBreak] ,
EventDetail[TimelinessBreakSort]
) ,
[RunningTotal*]
)
You can also try this (like your original measure), which is a bit more compact:
Pareto%* =
MAXX (
ALL (
EventDetail[TimelinessBreak] ,
EventDetail[TimelinessBreakSort]
) ,
[RunningTotal*]
)

Try:
I might be mistaken, though due to your definition of RunningTotal* you may have to use SUMMARIZE here:
=VAR T1 =
SUMMARIZE(
ALLSELECTED( EventDetail ),
EventDetail[TimelinessBreak],
"Running Total", [RunningTotal*]
)
RETURN
MAXX(
T1,
[Running Total]
)

Related

Same column Date difference based on other field DAX POWER BI

I want to calculate the date difference for all values in Field1 based on Field2 for example datediff(3/1/2020-2/1/2020) based on Field2 (c-b).
I have tried several ways but no luck so far.
there is no minimum mentioned in the question, so i assume that you are looking for the difference of the previous stages.
Date Difference =
DATEDIFF (
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date] < EARLIER ( 'Table'[Date] )
&& EARLIER ( 'Table'[Field1] ) = 'Table'[Field1]
)
),
'Table'[Date],
DAY
)
You can obtain a solution by adding 2 calculated columns:
First Calculated Column:
MinDate =
CALCULATE (
MIN ( YourTable[Date] ),
ALLEXCEPT ( YourTable, YourTable[Field1] )
)
2nd Calculated Column:
Date_Difference =
DATEDIFF ( [MinDate], [Date], DAY )
End Result:

DAX - Find and return the category value with the highest summarized measure

In the following example,
Highest Shipping Unit - should be the "ActualArea" where TotalPS has the maximum value.
This is a summarized table grouped by - Customer_ID, ProductDesignation,Package_CD,ActualArea
As you can see above I cannot produce the desired results. The DAX for HighestShippingUnit measure is as follows. I can't seem to figure out what I'm doing wrong.
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
VAR maxps =
MAXX ( tab, [GroupTotalPS] )
RETURN
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
DeviationReport is the name of my table in the dataset.
Please can you tell me what I'm doing wrong in my DAX or how to do it in a more efficient manner?
You were actually very close with your attempt.
If you change the final line from
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
to
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), [ActualArea] )
then it should work.
The reason is that using CALCULATE re-introduces the ActualArea filter context that you removed with ALLEXCEPT so that you only see the current ActualArea.
Thanks to #Alexis Olson. I managed to fix the problem.
The DAX is as follows -
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD],
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
RETURN
MAXX(TOPN(1, tab, [GroupTotalPS]), [ActualArea])
)

How to make running total ignore other columns DAX

I have a table in Power BI that calculates the running total. However, when I add additional columns to the table, the values get messed up. Any ideas on how get the running total to ignore specific columns?
Here is my code:
Measure =
CALCULATE (
SUM ( Append1[AVAILABLE] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <> MAX ( Append1[DUE DATE] )
)
)
+ CALCULATE (
SUM ( Append1[ORDER QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
- CALCULATE (
SUM ( Append1[REQUIREMENT QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
Below are pictures of what the table looks like when it runs correctly and what it looks like when I add another column and the values get messed up.
Correct running total:
Incorrect Running Total:
Thanks in advance for your help!
I was able to get it working correctly using ALLSELECTED. Thanks for your help.

How to calculate rank within Sales ranges

How to calculate rank within Category defined on sales level. Say, that we want to label products with Sales above some threshold with Category "high", and below that threshold with Category "low".
Here is a sample data.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcisqzSwpVtJRSiwoyEkF0oZKsTpIwkmJeUAIZJigipfn56QlpRYVVQLZpqhSyRlQcWOweFhqempJYlJOKlgusagovwS7XEF+SWJJPtwJKHL5eZn5eUDaHNUqHI5GdkEsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t, Product = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", Int64.Type}})
in
#"Changed Type"
My question is a nephew related to its older uncle, who now I want to call in:
Percent Rank within Category =
VAR HasOneValueTrue = HASONEVALUE ( MyTable[Product] )
VAR tbl =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
REMOVEFILTERS ( MyTable[Product] ),
VALUES ( MyTable[Category] )
)
VAR result =
CALCULATE (
DIVIDE (
RANKX (
tbl,
[Sales],
,
ASC
) - 1,
COUNTROWS ( tbl ) - 1
)
)
RETURN
IF (
HasOneValueTrue,
result
)
The difference is that the uncle has Category defined in table column, but now we want to have category calculated on the fly based on sales level. So I tried
replacing the VAR tbl code with the following one with the threshold of 4:
var tbl =
SUMMARIZECOLUMNS (
MyTable[Product],
"CalculatedCategory", IF ( [Sales] > 4, "high", "low" ),
"AggSales", [Sales]
)
Nevertheless, I am not able to refer to such defined variable. I also failed with trial based on creating first a DAX table and then trying to refer to it.
Here are expected results:
References
Here is the family of related questions which members I met on the way while approaching to state this problem.
DAX equivalent of Excel PERCENTRANK.INC per category
DAX RANKX for within Category
DAX REMOVEFILTERS vs ALL
The value parameter in DAX function RANKX
DAX ALLEXCEPT to sum by category of multiple dimension tables
This can be done with a minor modification to my answer here. Copied below:
Percent Rank =
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
ALLSELECTED ( MyTable[Product] )
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE ( RankProduct - 1, COUNTROWS ( ProductsInCategory ) - 1 )
)
First, define the calculated category as you suggested.
CalculatedCategory = IF ( [Sales] > 4, "high", "low" )
Then plug that into a filter in the ProductsInCategory variable.
Exp. Results =
VAR CalculatedCategory = [CalculatedCategory] /*Determine current category*/
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = CalculatedCategory /*New Condition*/
)
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE ( RankProduct - 1, COUNTROWS ( ProductsInCategory ) - 1 )
)
Output:
Edit:
To handle the case where there is only 1 product in a category, you can use MAX to disallow a zero value for the denominator.
Exp. Results =
VAR CalculatedCategory = [CalculatedCategory] /*Determine current category*/
VAR ProductsInCategory =
CALCULATETABLE (
VALUES ( MyTable[Product] ),
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = CalculatedCategory /*New Condition*/
)
)
VAR RankProduct = RANKX ( ProductsInCategory, [Sales],, ASC )
RETURN
IF (
HASONEVALUE ( MyTable[Product] ),
DIVIDE (
RankProduct - 1,
MAX ( COUNTROWS( ProductsInCategory ) - 1, 1 )
)
)
Being very grateful to Alexis Olson, I would like to share a different solution I ended up with. The solution proposed by Alexis works well in my simple example, but it did not work in my complex model. In my complex model the RANKX function does not give the expected results. RANKX returns the same rankings for different sales values.
For the time being this is the solution that works without figuring out what causes RANKX to return ties for different sales values.
First of all, defining Category measure:
CalculatedCategory =
SWITCH (
TRUE (),
NOT ( HASONEVALUE ( MyTable[Product] ) ), "total", -- important to handle totals
[Sales] <= 4, "low",
[Sales] > 4, "high",
"other"
)
It is important to exclude totals from Category. I did it by setting up a different category for totals. Otherwise totals will fall into "high" category bucket. It would distort final results.
I have not used RANKX in calculation of Percent Rank within Category. I used MIXTURE OF COUNTROWS and FILTER.
PercentRank within Category =
VAR category = [CalculatedCategory]
VAR ProductSales = [Sales]
VAR ProductsMatching =
COUNTROWS (
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = category
&& [Sales] >= ProductSales
)
)
var ProductsAll =
COUNTROWS (
FILTER (
ALLSELECTED ( MyTable[Product] ),
[CalculatedCategory] = category
)
)
RETURN
DIVIDE (ProductsMatching-1, MAX( ProductsAll-1, 1 ))
I calculated rows of two tables. First table ProductsMatching has all products that have sales in appropriate category and sales that are higher or equal of the product. ProductsAll returns number of products in category.

DAX - TOPN() in a SUMX() for multiple transactions

I need your help.
I am trying to make a calculated column which sum the sales for the last two dates (there are not sales on all dates!) for each product.
I have tried to use the calculation:
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[date_ID] ); 'Table'[Sale] );
ALLEXCEPT ( Table; 'Table'[Product_ID] )
)
But this only works if you have one sale per date per product ID.
What do I do if there are many transactions per date and prod ID?
Below I have a table where I have (filtered) one date and one product.
Now, I have made a calculated column that sums all transactions per date and product ID (for reference).
This is
Calculated Column 2 =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; 'Table'[Date]; 'Table'[Product_ID] )
)
Now comes the difficult part.
If I want to sum the transactions for the last two dates on each product, I have made the following calculation:
Calculated Column 1 =
VAR SumProd =
CALCULATE (
SUMX ( Table; Table[Sale] );
ALLEXCEPT ( Table; Table[Date]; Table[Product_ID] )
)
RETURN
CALCULATE (
SUMX ( TOPN ( 2; 'Table'; 'Table'[Date] ); SumProd );
ALLEXCEPT ( Table; Table[Product_ID]; Table[Date] )
)
The Problems:
The calculations sums ALL the values in each category
Example: in the table, you see "Calculated column 1"=7571200, which is 27040 * number of transactions? I only want the value 27040.
For some reason, the TOPN() doesn’t work. If I change the N_Value=2 to N_Value=3, the calculation doesn’t change?
Please, does anyone know what is wrong with my calculation?
Thanks.
Br,
Jakob
In your measure, SumProd is a constant that you're summing for each row in the table. This is not what you want.
I'd suggest something more like this:
Calculated Column 1 =
VAR LastTwoDates =
CALCULATETABLE (
VALUES ( Table[Date] );
TOPN ( 2; ALLEXCEPT ( Table; Table[Product_ID] ); Table[Date] )
)
RETURN
CALCULATE (
SUM ( Table[Sale] );
ALLEXCEPT ( Table; Table[Product_ID] );
Table[Date] IN LastTwoDates
)
In this, we first calculate a list of the top 2 dates associated with Product_ID and store it as a variable. Then we use that as a filter when calculating the sum of Sale.
I'm not positive this exact syntax will work, but I hope it will point you in a better direction.
It turns out my code didn't work exactly as intended. Try this instead:
Calculated Column 1 =
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table1[Date] );
ALLEXCEPT ( Table1; Table1[Product_ID] )
)
VAR LastTwoDates = TOPN ( 2; ProductDates; [Date] )
RETURN
CALCULATE (
SUM ( Table1[Sale] );
ALLEXCEPT ( Table1; Table1[Product_ID] );
Table1[Date] IN LastTwoDates
)