Sum MAX values of a column - powerbi

Situation:
I have a column (refund) that takes the MAX value for each day so that I don't duplicate the refunds.
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
This works for each day but the total is the max of that column
Objective:
I need this measure to calculate the net sales. I have the gross sales measure already but my refund formula doesn't work when aggregated.
What i tried (thanks to Alexis Olson):
refund =
var calcul =
CALCULATE(
SUM(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)
return
SUMX(DISTINCT(Query1[orderId]), calcul)
However the output is unexpected. It basically takes each value and multiplies it by the distinct count of order ids on that day (refund or not).
So i tried dividing it by the distinct count of order id but the same problem with the total row taking the max value occurs.
Here's the output i get using the provided solution below:
Relationships:
Query1[created_at] DateTable[Date] (inactive)
Query1[orderDate] DateTable[Date] (active)
My returns measure:
Returns =
CALCULATE(
MAX(Query1[amount]),
USERELATIONSHIP(Query1[created_at], DateTable[Date]),
Query1[kind] = "refund",
Query1[status] = "success"
)

You're pretty close to something that works. Try changing calcul to a summary table instead of a single value scalar like this:
refund =
VAR Summary =
SUMMARIZE (
Query1,
Query1[orderId],
"MaxValue", CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[created_at], DateTable[Date] ),
Query1[kind] = "refund",
Query1[status] = "success"
)
)
RETURN
SUMX ( Summary, [MaxValue] )

One way of fixing my issue was to use CALCULATETABLE before the summarize and then
use the SUMX as suggested above by Alexis.
Returns =
VAR sumary =
CALCULATETABLE(
SUMMARIZE(
Query1,
Query1[orderId],
"maxValue",CALCULATE(
MAX(Query1[amount]),
Query1[kind]= "refund",
Query1[status] = "success"
)
),USERELATIONSHIP(Query1[trx_date],DateTable[Date]))
RETURN
SUMX(sumary,[maxValue])

Related

calculatation day over day

Hi I need to calculate day over day
I attached the pbix file https://1drv.ms/u/s!Amd7BXzYs7AVhBBCo_Ls7q5IkrXH?e=d1nNmA 
I've tried the following calculated measure but actually it returns wrong values, it returns sales for current date, but I need to return sales for previous day. How to correct it?
sales day over day = var _maxdate=CALCULATE(max('Date'[Date]), ALLSELECTED('Date'[Date]))
var _mindate=CALCULATE(min('Date'[Date]), ALLSELECTED('Date'[Date]))
var dates=filter(values('Date'[Date]), 'Date'[Date]<=_maxdate && 'Date'[Date]>=_mindate)
return if(ISEMPTY('Date'), BLANK()
, calculate(sum(Sales[Sales]), FILTER(Sales, Sales[Date]<=_maxdate && Sales[Date] >=_mindate && PREVIOUSDAY('Date'[Date])))+0
)
It seems that your measure works. I've got sales for previous days.
You may need to use simple measures as follows:
TotalSales =
SUM ( Sales[Sales] )
PreviousDaySales =
CALCULATE ( [TotalSales], PREVIOUSDAY ( 'Date'[Date] ) )
DayOverDayChange =
DIVIDE ( [TotalSales], [PreviousDaySales], 0 )
The result (not on your data):

SELECTED Value from slicer in not filtering previous month results

I have a dataset like below and I joined it with a calendar table on a created date.
I am trying to do an avg daily cases MoM analysis, I am comparing the current month vs. the Previous month's avg daily cases.
For example, IF I got 60 distinct cases in May, and my daily avg cases for May is 2 cases per day. I want to calculate the same for the previous month when I select Month from the filter.
Here I selected May-2021. I want my measure to calculate Apr-2021 Avg Daily cases.
So I created two measures, one is for the selected month, and another one is for prior avg daily cases.
Avg Daily Cases =
VAR Selected_Month =
SELECTEDVALUE ( 'Date'[MMM-YYYY] )
VAR Month_Days =
CALCULATETABLE (
VALUES ( 'Date'[DateAsInteger] ),
FILTER ( 'Date', 'Date'[MMM-YYYY] = Selected_Month )
)
RETURN
CALCULATE (
AVERAGEX ( Month_Days, CALCULATE ( DISTINCTCOUNT ( MyTable[CaseNo] ) ) )
)
Prior Month Avg Daily Cases
Avg Daily Cases Prev Month =
VAR Selected_Month =
SELECTEDVALUE ( 'Date'[MMM-YYYY] )
VAR Last_Month_Days =
CALCULATETABLE (
VALUES ( 'Date'[DateAsInteger] ),
FILTER (
'Date',
MONTH ( 'Date'[Date] )
= MONTH ( Selected_Month ) - 1
&& 'Date'[Year] = YEAR ( Selected_Month )
)
)
RETURN
CALCULATE (
AVERAGEX ( Last_Month_Days, CALCULATE ( DISTINCTCOUNT ( MyTable[CaseNo] ) ) )
)
Avg Daily Cases measure working fine and getting the expected results, however, Avg Daily Cases Prev Month is not working.
Suppose you have total case of 30 in May and 60 in June, and you have the following dataset in order to return comparison for two using single selection and accept the answer if helping :)
First I have create the following table for selection
Current period average case
Current daily case =
var caseTotal = CALCULATE(DISTINCTCOUNT(Sheet1[Case]),
FILTER(Sheet1,Sheet1[Period] in {SELECTEDVALUE(Selection[Selection])}))
return
caseTotal / DAY(EOMONTH(SELECTEDVALUE(Selection[Selection]),0))
Prior comparison
Prior daily case =
var caseTotal = CALCULATE(DISTINCTCOUNT(Sheet1[Case]),
FILTER(Sheet1,Sheet1[Period] in {FORMAT(EDATE(SELECTEDVALUE(Selection[Selection]),-1),"mmm-yyyy")}))
return
caseTotal / DAY(EOMONTH(EDATE(SELECTEDVALUE(Selection[Selection]),-1),0))
Result:

PowerBI DAX for Finding Last Record

Question
What is an efficient way to create a calculated column finding the last value of my DATE column, using the ModifiedOn column, per ID? I don't want the MAX date, just the last record (even if the last record is the minimum). Also, my table is a calculated column.
Example Table
ID
DATE
ModifiedOn
A
2/4/2020
1/16/2019
A
2/5/2020
1/17/2019
B
3/2/2020
2/7/2020
B
3/3/2020
2/8/2020
B
3/1/2020
2/9/2020
Current Formula
LastRecord =
VAR Max_Date =
CALCULATE (
MAX ( 'Table1'[ModifiedOn] ),
ALLEXCEPT ( 'Table1', 'Table1'[ID] )
)
RETURN
IF (
Table1[ModifiedOn] = Max_Date,
Table1[DATE]
)
Current Results
But using the formula I get a calculated column that looks like this:
I keep getting blanks where they should be filled with the LAST recorded date of that ID.
Use the following dax formula to create the expected column:
Column =
VAR __id = 'Table'[ID]
VAR __lastMod =
CALCULATE(
MAX( 'Table'[ModifiedOn] ),
FILTER( 'Table', 'Table'[ID] = __id )
)
VAR __lastDate =
CALCULATE(
MAX( 'Table'[Date] ),
FILTER( 'Table', 'Table'[ID] = __id && 'Table'[ModifiedOn] = __lastMod )
)
Return __lastDate

DAX Measure: group by min & if condition

I have several dataset tables in PowerBI report. The column country comes from TABLE1 while the column name comes from TABLE2.
So firstly I want to calculate min_number based on country and name, and then if min_number = number, the min will be 1; otherwise, 0. So the result table looks like:
This is my code for min
min =
VAR min_number =
CALCULATE (
MIN ( [number] ),
ALLEXCEPT ( TABLE1, TABLE1[country] ), ALLEXCEPT (TABLE2, TABLE2[name])
)
RETURN
IF ( [number] = Min_number,1, 0 )
I got an error: the MIN function only accepts a column reference as the argument number 1. Does it mean if it has to be one condition? how to fix it? Thank you
I would solve it by just making two separate measures, since we want to see the both results in the final table anyway.
First the min_number calculation:
min_number = CALCULATE(MIN('Table'[number]);ALLEXCEPT('Table';'Table'[country];'Table'[name]))
And the min measure:
min = IF(MAX('Table'[number]) = [min_number];1;0)
As we are using a measure, we can use MAX, so it will know what number to reference in the IF. It will still use the MAX number per row, so results are correct.
You can try with this below measure-
min =
VAR current_row_country = MIN(table1[country])
VAR current_row_name = MIN(table1[name])
VAR current_row_number = MIN(table1[number])
VAR min_number =
CALCULATE (
MIN (table1[number]),
FILTER(
ALL(table1),
table1[country] = current_row_country
&& table1[name] = current_row_name
)
)
RETURN IF (min_number = current_row_number,1, 0 )

How to create a DAX calculated column in PowerBi that finds the MIN value based on another columns FIRSTDATE?

Need some help to calculate the following in two separate columns in a DAX formula
Earliest Reading for each equip
Latest Reading for each equip
Screenshot of sheet
I've been able to get the first date of each equip with this.
CALCULATE(FIRSTDATE(Transactions[Date]),ALLEXCEPT(Transactions,Transactions[Equip No]))
But cannot work out how to get the 'Reading' value that is associated with the first date
I've managed to do this with a measure, but would also like to get this in a calc. column.
Latest Reading =
SUMX (
VALUES( Transactions[Equip No] ),
CALCULATE ( MIN ( Transactions[Reading] ), FIRSTDATE ( Transactions[Date] ) )
)
this should help you.
Earliest Column
Earliest =
VAR __equipNumber = 'Transaction'[Equip No] //Get the Equip No to filter the main table and create an auxiliar table for every different Equip No.
VAR __minDate = CALCULATE( MIN('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) ) //Get the lowest date asociated to every Equip No.
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __minDate ) //Create a table that contains 1 row asociate to the lowest date.
Return CALCULATE(SUM('Transaction'[Reading]), __subTable) //Operate over the auxiliar table to get the expected value.
Latest Column
Latest =
VAR __equipNumber = 'Transaction'[Equip No]
VAR __maxDate = CALCULATE( MAX('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) )
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __maxDate )
Return CALCULATE(SUM('Transaction'[Reading]), __subTable)
I obtained the expected result