total issue with previous day - powerbi

I calculate dayd over day. the measure returns correct values, but the total always 0 which is wrong, 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]), PREVIOUSDAY('Date'[Date]))+0)
I attach the pbix file https://1drv.ms/u/s!Amd7BXzYs7AVhBBCo_Ls7q5IkrXH?e=1ZQzsE

I'm not familiar with powerBI, but I suppose when column 'Date' is empty, blank() is returned but such value is not an illegible value to calculate 'total'. Try to remove these rows, or assuming an alternative date for that cases

Related

calculated column stops working when exdternal filter used

I have some Fact Revenue, I am trying to group by Conca, and display the values only if negative…
For doing it I have this calculated column:
=
VAR name1 = Revenue[Conca]
VAR name2=
CALCULATE (
SUM ( Revenue[CostOfQuality] ),
FILTER ( Revenue, Revenue[Conca] = name1 )
)
RETURN
if (name2>0, 0, Revenue[CostOfQuality])
It works:
(highest value is 0 as expected):
Now...
If I drag fiscal year it stops working:
Why is it that I see numbers higher than 0?? (I want it to still work even if I bring other filters...)
a calculated column is computed computed once on the whole dataset after the data are loaded, and is basically a "static column".
Whatever the expected result, you are looking for a measure, which gets computed in the current context
Calculated columns and calculated tables are different from measures, since they are static: You can filter them, but they don't recalculate on filter changes.

HR Employee Count with Slicing by Dimensions - Slowly Changing Dimension

I need to calculate headcount while keeping the measure sliceable by any dimension connected to the fact table. Given the nature of my tables and model, what I need to do is a point in time calculation on a Slowly Changing Dimension Type 2.
I managed to make it work using the function KEEPFITLERS, but I need a more scalable function that wouldn't require me to list all the dimensions I want to slice by.
Here is my PowerBI file with sample data: https://gofile.io/d/smS2Hr
Here is a simplified sketch (image) of my model: https://ibb.co/fQYpsdx
Background:
The first measure I am calculating is the number of Employees at the Start of the Period (Employees SoP). If the end-user selects in PowerBI the whole month of January 2020, the Start of the Period is Jan 1st, 2020. Hence, "Employees SoP" for the month of Jan 2020 will give the number of employees on Jan 1st, 2020.
The formula below calculates the correct values for Employees SoP:
Employees SoP =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result
The problem with the formula above is that, because of the ALL function, the measure is not sliceable by any dimension, i.e., Pay Class and Employment Status (the same number repeats itself).
Results:
Hence, I created this other measure using KEEPFILTERS, and it works perfectly.
Employees SoP KEEPFITLERS =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
), KEEPFILTERS(PayClass), KEEPFILTERS(EmploymentStatus))
RETURN
Result
The problem with this formula is that I have to list all the dimensions I want to slice by ( e.g., PayClass, EmploymentStatus) inside the DAX formula. This is not very scalable.
I did some experimenting with REMOVEFILTERS but it looks like it does not work with DirectQuery for now, so it wouldn't solve my production problem. Link:
https://learn.microsoft.com/en-us/dax/removefilters-function-dax
QUESTION:
How can I write this measure using an alternative to KEEPFILTERS with which I wouldn't have to list each dimension I want to slice by?
Thank you!
I just managed to solve my problem. I made both relationships to the Dates table "inactive". With that, I could remove the "ALL" function in the DAX formula and now it not only calculates the correct values, but it also slices nicely. I will have to use the USERELATIONSHIP function to calculate more complex measures but, in most of the cases, this simple solution works like a charm.
Employees SoP without ALL =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER('Fact', 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result

Creating an measure to compare week-over-week developments

My dataset includes information from at 26 different weeks. It lists all the open items from an accounts receivable database for each of the 26 weeks. Each of the report dates is exactly 7 days apart.
I am trying to compare the current receivables with the amount of the last week.
I thought that I will just extract the last report date with
LastReport:=LASTDATE(Report Date)
which gave me indeed the last report date. I go back 7 days with
PriorWeek:=DATEADD(LastReport;-7;DAYS).
This worked fine.
However, when I try to calculate the sum of last week using
CALCULATE(SUM(Total AR);Reportdate=PriorWeek)
I can an error that I cannot compare date and text fields.
I have checked the report date column is set to date.
What am I doing wrong?
I would say 'No need of ranking the Dates'. My solution is below using calculated columns:
Amount Variance =
VAR _PrevBlank =
ISBLANK ( [PrevWeek Amount] )
VAR _Amount = [Amount]
VAR _PrevAmount = [PrevWeek Amount]
VAR _Variance =
IF ( _PrevBlank, 0, _Amount - _PrevAmount )
RETURN
_Variance
I would suggest creating a date index using RANKX
RankDate = RANKX(Table1,Table1[Report Date],,ASC)
Then you can either create a calculated column that holds the previous week value
PreviousWeekCol = LOOKUPVALUE(Table1[Total AR],Table1[RankDate],Table1[RankDate]-1)
Or create a calculated measure that holds the prior week value
PreviousWeekMeasure =
VAR MaxDateIndex = MAX(Table1[RankDate])
RETURN CALCULATE(SUM(Table1[Total AR]),Table1[RankDate]=MaxDateIndex-1)

DAX efficient measure to get max date for max date and blanks for other dates

I would like to get the latest date of FactTable column in both ways (1) repeated all across table and (2) just in the max date.
The first:
DateMax Repeated Measure = CALCULATE(MAX(FactTable[Date]), ALLSELECTED('FactTable'))
The second:
DateMax Only Measure =
VAR GetMaxDate = CALCULATE( MAX(FactTable[Date]), ALLSELECTED('FactTable'))
return
CALCULATE( MAX(FactTable[Date]), FILTER('Calendar', 'Calendar'[Date]=GetMaxDate))
The second measure (as desired) returns all blanks except the single date where date is max. Is there no efficient way then I did it?
You shouldn't need to use CALCULATE a second time. You should just be able to do something like
IF(MAX('Calendar'[Date]) = GetMaxDate, GetMaxDate, BLANK())

DAX SUM previous period integer value

This issue seemed to be easy at the first glance but I have been trying to solve it for a while.
enter image description here
I would like to dynamically sum the previous period sales in a power pivot measure. The thing is that my period column is a integer value not a date.
I manage to calculate the previous period but I did not manage to set it up as a filter value:
Max(Table1[Period])-1 --> this gives me the previous value of the period field
However when I want to add this as a filter of a calculated measure it doesn't work: --> Calculate(
Sum(table1[Sales]), Filter(table1,Max(table1[Period])=Max(table1[Period])
)
I tried simply this one as well: Calculate(Sum(table1[Sales]), table1[Period] = table1[Period] -1 )
but neither of them are working. I though that I do it with calculated column however I would rather do it with measure.
Can you please help me?
Expected result:
Create measure:
Previous Sales:=
CALCULATE( SUM(Table1[Sales]),
FILTER( ALL(Table1), Table1[Period] = MAX(Table1[Period]) - 1))
It will give you dynamic previous sales. Please note: it relies on the fact that periods increment by 1.
If you need to summarize Previous Sales, create a second measure:
Total Previous Sales:=
SUMX( VALUES(Table1[Period]), [Previous Sales])