Power Bi DAX: Cumulative Sum not Showing Correct Data - powerbi

I am having trouble making a cumulative sum.
It is showing data from the previous column instead of being a cumulative.
I have tested it out on a test file and it works fine but using the same format of the formula:
QuoteValue running total in ClosedDate =
Sales running total in Date =
CALCULATE(
SUM('Sheet1'[Sales]),
FILTER(
ALLSELECTED('Sheet1'[Date]),
ISONORAFTER('Sheet1'[Date], MAX('Sheet1'[Date]), DESC)
)
)
Im not sure what i am doing wrong, ive used ALLSELECTED, FILTER(ALL, ALL
The Quotevalue and Date are both in the same table and all other fields im using in the visual are all in the same table.
Im stumped.
Here is a dummy sheet i have put together.
Dummy Sheet

Sales running total in Date =
var person = 'Sheet1'[Person]
var toDate = 'Sheet1'[Date]
return CALCULATE(
SUM('Sheet1'[Sales]),
FILTER('Sheet1', 'Sheet1'[Person] = Person && 'Sheet1'[Date] <= toDate)
)
What we do here is that for each row, we get the other rows where persons are same and date is lower or equal. From this we calculate the sum.

Related

need assistance\ideas on building what is hopefully a simple DAX measure

I am trying to figure out the DAX to create a new measure in an SSAS Tabular data model. An example of what I am trying to do is more easily shown than described. My SSAS Tabular dataset produces the following table. Cols A and B are from the stores table, Col C is a measure from the Sales table, Col D is a measure from the Products table, and Col E is C/D. This all works fine. Data has been mocked up in Excel to protect the innocent, but it is working in Power BI.
What I would like to do is add a new measure which calculates the Sales/Product at the state level and have that measure show for each store in that state, as shown below
Presumably I have to iterate over all rows and calculate the total sales/state and total products sold/state and divide those 2 to get the answer, but can't work out the DAX to get there. I have tried numerous combinations of
calculate(
sumx(...),
filter(
all(...),
...
)
)
to no avail.
You should use FILTER with ALL to manipulate a context(remove current context);
MesureSumStateLevel = calculate(SUM('Table'[Amount]),
FILTER(ALL('StoreStateTab'), 'StoreStateTab'[State] =
SELECTEDVALUE('StoreStateTab'[State])))
https://dax.guide/filter/
https://dax.guide/selectedvalue/
https://dax.guide/all/
Thanks for the tip. I originally tried that and dropped it because I couldn't get it working. I revisited this morning and solved it. Here is what I did:
State Ttl =
var trxYr = convert(SELECTEDVALUE(dim_date[Year]), INTEGER) //needed because Year is stored as text in the model
var trxMo = SELECTEDVALUE(dim_Date[Short Month Name])
var trxState = SELECTEDVALUE(fact_Sales[state])
Return
CALCULATE(
SUM(fact_sales[SalesAmt])
,all(fact_sales)
,year(fact_sales[SaleDATE]) = trxYr
,dim_Date[Short Month Name] = trxMo
,dim_Stores[state] = trxState
)

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

PowerBI DAX: How to calculate a running total for both date and another filter

I need to calculate a running total of a measure by date and by source. Below is a screenshot how my table looks like now.
Below is the measure code.
CashPosition_Revenue Running Total = CALCULATE([CashPosition_Revenue],FILTER(VALUES(FilterKeys_Date[Date]), FilterKeys_Date[Date] <= MAX(FilterKeys_Date[Date])))
I need last column to be calculated as running total.
Using Values limits your filter context to the current date only, try using All to remove that filter:
CashPosition_Revenue Running Total =
VAR CurrentDate = MAX(FilterKeys_Date[Date])
RETURN
CALCULATE(
SUM(FilterKeys_Date[CashPosition_Revenue]),
FILTER(
ALL('FilterKeys_Date'),
FilterKeys_Date[Date] <= CurrentDate
)
)

How to calculate Cumulative Sum in Power BI

I know how to do cumulative sum if dataset has Dates, But I am struggling to do same if I do not have dates in my dataset.
Below is the data, I want CUM Sales
I've selected New quick measure -> Totals -> Running total and creates this:
sales running total in part =
CALCULATE(
SUM('Query1'[sales]);
FILTER(
ALLSELECTED('Query1'[part]);
ISONORAFTER('Query1'[part]; MAX('Query1'[part]); DESC)
)
)
Returns:
You can also create own measure to calculate cumulative sum.
Select New Measure from the ribbon and write the following expression
Cumm Sales =
VAR Current_Part = MAX(Test[Part])
RETURN
CALCULATE(
SUM(Test[Sales]),
Test[Part]<=Current_Part,
ALL(Test[Part])
)
Output:

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)