Error in last 3 months calculation in PowerBi - powerbi

calculating the last 3months or average was working in one PBIX file.
Now when I try to use it in another it comes up empty.
Naming seems to be right and the Calendar is a copy from the working PBIX.
What can have gone wrong between the two files, is there something obvious you can see?
Is there something I should check and possibly change?
SLS L3m (USD) =
var months = 3
var sum_period =
CALCULATE(
[Sales (USD)],
DATESINPERIOD('Calendar'[Date] , FIRSTDATE('Calendar'[Date])+1 , -months, MONTH )
)
return
IF( NOT(ISBLANK([Sales (USD)])), sum_period)
INV avg L12m (USD) =
var months = 12
var sum_period =
CALCULATE(
[Inventory (USD)],
DATESINPERIOD('Calendar'[Date] , LASTDATE('Calendar'[Date]) , -months, MONTH )
)
return
IF( NOT( ISBLANK( [Inventory (USD)] )), sum_period/months )
enter image description here

Related

AND function in Power BI

I'm trying to create a new mesure in order to create a gantt chart, but the dax field keeps spitting that there's an error. Where in fact i literally copied the same code in the tutorial i'm following.
And the new mesure is as follows:
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date ],
REMOVEFILTERS(TabelaCalculada)
), var EndDate =
CALCULATE(
min(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var ProjectPeriod =
min(TabelaCalculada[Date]) >= startdate
&& min(TabelaCalculada[Date]) <= startdate
var result =
if(
TabelaCalculada,
1
)
return
You see PBI is saying the following: "the syntax for '&&' is incorrect", i even searched the documentation for this particular AND method but i don't see how it's wrong.
This DAX is invalid in multiple ways which I point out on the assumption that you are trying to learn what valid DAX is.
Comma after the definition of var startdate.
Project Period is a bunch of filters with no actual function.
Project Period is trying to filter on an aggregate, which is not allowed.
return statement is missing the return argument
var result is incomprehensible.
This will return a 1 for each row where the date falls between minimum Start Date and maximum End Date, per your latest comment.
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date]),
REMOVEFILTERS(TabelaCalculada)
)
var EndDate =
CALCULATE(
max(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var result = if(
selectedvalue(TabelaCalculada[Date] >= startdate
&& TabelaCalculada[Date] <= EndDate,1)
)
return result
I've found a third syntax alternative that worked. So my goal as i explained was to create a Gantt Chart using the matrix innate visual and for that one way to do it is to create a measure that returns a 1 if i have a date that's either on or after the starting date and on or before the ending date.
BetweenDates =
VAR beginning = min ( Contracts_range[Start Date])
VAR finish = max ( Contracts_range[End Date] )
VAR duration = max ( Contracts_range[Duration] )
VAR colorVar = 0
RETURN
SWITCH (
TRUE(),
AND ( MAX ( 'Calendar'[Date] ) >= beginning , MIN ( 'Calendar'[Date] ) <= finish ) && duration >0,1
)

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

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

An Argument of Function Date has wrong data type or result is too small or too large Error

Getting the error
An Argument of Function Date has wrong data type or result is too small or too large
when the following measure is included in any graph. However, the Value is shown properly in KPI.
LastYear =
VAR lastFromDate =
DATE ( YEAR ( [From_Date] ) - 1, MONTH ( [From_Date] ), DAY ( [From_Date] ) )
VAR lastToDate =
DATE ( YEAR ( [To_Date] ) - 1, MONTH ( [To_Date] ), DAY ( [To_Date] ) )
RETURN
IF (
[DateDiff] > 365,
0,
SUMX (
FILTER (
ALL ( WUSA_CAL_DIM ),
WUSA_CAL_DIM[End_Date] >= lastFromDate
&& WUSA_CAL_DIM[End_Date] <= lastToDate
),
[Sales_Value]
)
)
I want to show last year sales based on from and to date in the slicer. Removing -1 solves the problem but it doesn't show previous year sales in that case which is needed.
I think this has to do with a lack of context around the [From_Date] and [To_Date] in your Variables. However, without access to your source data and not knowing anything about your datamodel due to lack of context I'm making huge assumptions here.
PowerBI ( or rather the DAX ) has no idea which set of dates you want it to use.
Attempt something like:
LastYear =
VAR lastFromDate =
SELECTEDVALUE(From_Tbl[From_Date], TODAY()) - 365
VAR lastToDate =
SELECTEDVALUE(To_Tbl[To_Date], TODAY()) - 365
RETURN
IF (
[DateDiff] > 365,
0,
SUMX (
FILTER (
ALL ( WUSA_CAL_DIM ),
WUSA_CAL_DIM[End_Date] >= lastFromDate
&& WUSA_CAL_DIM[End_Date] <= lastToDate
),
[Sales_Value]
)
)
I came across this issue today, its late but, It has to do with Blank dates present somewhere in the column.
In this Instance,
DATE ( YEAR ( [From_Date] ) - 1, MONTH ( [From_Date] ), DAY ( [From_Date] ) )
if From_Date is BLANK, it is one of the reason for the error.
Suggested Alternative,
IF( ISBLANK([From_Date]), BLANK(), DATE ( YEAR ( [From_Date] ) - 1, MONTH ( [From_Date] ), DAY ( [From_Date] ) ))
The simple solution for that is to understand are you applying -1 (day before) to the day or to the full date (yyyy,mm,dd) or just to the day (dd). 
For Example: If you are applying -1 to the day and the today is 1st of the month it througn an error as it try to find 1-1 with no date reference where if you apply end of the date that work absolutely fine even for the 1st of the month. 
If you want to get last 12 months dates
Correct solution is -
VAR Days = CALENDAR(DATE(YEAR(TODAY())-1,MONTH(TODAY())-1,DAY(TODAY())),DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())))
If you want get the last year same date + 1 day before
VAR Days = CALENDAR(DATE(YEAR(TODAY())-1,MONTH(TODAY())-1,DAY(TODAY()))-1,DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())))
However this give you an error if you add -1 after the day shown below
Wrong approach this thrown an error date is too small or too large
VAR Days = CALENDAR(DATE(YEAR(TODAY())-1,MONTH(TODAY())-1,DAY(TODAY())-1),DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())))

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