calculatation day over day - powerbi

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):

Related

DAX Formula to filter the last 28 days in the table data

I have a measure that totals the values for each date in the table. I want to filter this measure so that I can display only the last 28 days present in the table instead of displaying values for all the dates. Following is the code that works for getting totals for full table:
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) ) )
The 'relative date' filter in the Filters pane does not work because it only accepts the last 28 days based on today's date and not the dates in the table. Please suggest a DAX formula that can filter for the last 28 days present in the table.
Try this code
VAR endDay = LastDate(Daily_Reports[Case_Date])
VAR startDay= DATEADD(endDay,-28,DAY)
VAR setOfDates = DATESBETWEEN(Daily_Reports[Case_Date], StartDate, EndDate )
RETURN
CALCULATE(
SUM(Daily_Reports[Confirmed])
,setOfDates
)
You can try this one:
MS =
CALCULATE (
SUM ( Daily_Reports[Confirmed] ),
FILTER (
ALL ( Daily_Reports[Case_Date] ),
Daily_Reports[Case_Date]
>= SELECTEDVALUE ( Daily_Reports[Case_Date] ) - 28
)
)
This is what finally worked for me. I created a measure (not a column), that returns 1 for the last 28 days with an IF clause, leaving it blank if the date is not in the last 28 days as follows:
Last28 =
VAR MaxDate = LASTDATE( ALL(Daily_Reports[Case_Date]) )
VAR MinDate = DATEADD( MaxDate, -28, DAY )
RETURN
IF( SELECTEDVALUE(Daily_Reports[Case_Date]) >= MinDate && SELECTEDVALUE(Daily_Reports[Case_Date]) <= MaxDate, 1 )
Then I incorporated this measure into the Calculate function as follows:
Daily Cases =
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) && NOT(ISBLANK(Daily_Reports[Last28]))
)
)

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:

count days between two dates not working correctly

I have slicer date in report. I use the following expression in order to calculate the number of days between two selcted dates
Remaining Days =
VAR a =
FIRSTDATE (MyTimeView[CC_DT_DATE])
VAR b =
CALCULATE ( MAX ( MyTimeView[CC_DT_DATE] ); ALLSELECTED (MyTimeView[CC_DT_DATE]) )
RETURN
DATEDIFF ( a;b; DAY )
When I select 01/01/2020 to 31/01/2020, I am expecting to get 31 days but I get 29 days.
How to modify the expression,?
DATESBETWEEN should work.
Something like:
Remaining Days =
CALCULATE(
COUNTROWS(MyTimeView),
DATESBETWEEN(
MyTimeView[CC_DT_DATE],
MIN(MyTimeView[CC_DT_DATE]),
MAX(MyTimeView[CC_DT_DATE])
)
)

Sum MAX values of a column

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])

Project revenue calculation in DAX

I need to calculate the ongoing revenue for installation + maintenance for projects and calculate the monthly revenue for controlling purposes in DAX in Power BI.
The problem is the following.
The projects are stored in a table CONTRACTS like this:
And I have a separate date table INST_DATE_TABLE:
The tables are connected via the [INSTALLATION_DATE] field.
For every month the revenue is the total of the [INSTALLATION_REVENUE] if the installation was carried out that month plus from the first month on the monthly maintenance revenue which is given as the [MAINTENANCE_COST_PER_UNIT] * [MAINTENANCE_UNIT] / 12.
And the maintenance revenue should be only calculated if the current date is beyond the installation date!
Some contracts are not yet signed, so they dont't have an installation date set (NULL)
So the INSTALLATION REVENUE DAX is like this:
.INSTALLATION_REVENUE =
CALCULATE (
SUMX(CONTRACTS;
CONTRACTS[INSTALLATION_REVENUE]
);
CONTRACTS[INSTALLATION_DATE] > 0
)
And the MONTHLY REGULAR REVENUE is like this:
.REGULAR_REVENUE =
CALCULATE (
SUMX(CONTRACTS;
CONTRACTS[MAINTENANCE_COST_PER_UNIT]*CONTRACTS[MAINTENANCE_UNIT]
) / 12;
CONTRACTS[INSTALLATION_DATE] > 0
)
For all dates I can calculate the cash flow of the latter like this:
.REGULAR_REVENUE_ONGOING =
CALCULATE (
[.REGULAR_REVENUE];
ALL(INST_DATE_TABLE[INSTALLATION_DATE])
)
which gives me a nice series of monthly revenues for all periods. But I only would like to see this for the periods that are beyond the installation date!
So lets say filtered on contract 1 I have now the following cash flow:
But for periods before 2019.04.01 I would like to see zeros!
How can I do that?
I just can't filter on dates referring to the installation date of the project!
After I have the expected result for one contract it would be easy to sum it up for all contracts like this
.TOTAL_REVENUE =
[.INSTALLATION_REVENUE] + [.REGULAR_REVENUE_EXPECTED]
UPDATE:
I created a cumulative total to display the ongoing revenue as such:
.REGULAR_REVENUE_ONGOING =
CALCULATE (
[.REGULAR_REVENUE];
FILTER(
ALL(INST_DATE_TABLE[INSTALLATION_DATE]);
INST_DATE_TABLE[INSTALLATION_DATE
<=MAX(INST_DATE_TABLE[INSTALLATION_DATE])
)
)
this displays the correct series, but now I have another problem. When I try to cumulate this already cumulative data series it does not add up as a cumulative data series!
Any help would be appreciated
.REVENUE_TOTAL_CUMULATIVE =
CALCULATE(
[.REVENUE_TOTAL];
FILTER(
INST_DATE_TABLE;
INST_DATE_TABLE[INSTALLATION_DATE] <= MAX(INST_DATE_TABLE[INSTALLATION_DATE])
)
)
Assuming there are no end dates to your ongoing revenue, then try:
.REGULAR_REVENUE_ONGOING =
VAR DateMin =
CALCULATE(
MIN ( CONTRACTS[INSTALLATION_DATE] ),
ALL ( INST_DATE_TABLE )
)
VAR DateMax =
MAX ( INST_DATE_TABLE[INSTALLATION_DATE] )
RETURN
SUMX (
FILTER (
ALL ( INST_DATE_TABLE ),
INST_DATE_TABLE[INSTALLATION_DATE] >= DateMin && INST_DATE_TABLE[INSTALLATION_DATE] <= DateMax
),
[.REGULAR_REVENUE]
)
And for a cumulative total revenue:
.REVENUE_TOTAL_CUMULATIVE =
VAR DateCurrent = MAX ( INST_DATE_TABLE[INSTALLATION_DATE] )
VAR CumulativeInstallationRevenue =
CALCULATE (
[.INSTALLATION_REVENUE],
FILTER (
ALL ( INST_DATE_TABLE ),
INST_DATE_TABLE[INSTALLATION_DATE] <= DateCurrent
)
)
VAR CumulativeOngoingRevenue =
SUMX (
FILTER (
ALL ( INST_DATE_TABLE ),
INST_DATE_TABLE[INSTALLATION_DATE] <= DateCurrent
),
[.REGULAR_REVENUE_ONGOING]
)
RETURN
CumulativeInstallationRevenue + CumulativeOngoingRevenue
See https://pwrbi.com/so_55808659/ for worked example PBIX file