How to calculate a simple running total? - powerbi

This is table: Customer
And this is table Transactions:
They are related by No.
I want the running total of Esaldo:
So:
Running total =
CALCULATE(
SUM(Transactions[ESaldo]),
FILTER(
ALL(
Transactions),
Transactions[Date] <= MAX(Transactions[Date])
)
)
This is wrong:
Due to the external filter (slicer).
So, this should do it:
Running total 2 =
CALCULATE(
SUM(Transactions[ESaldo]),
FILTER(
ALLEXCEPT(Transactions, Transactions[no]),
Transactions[Date] <= MAX(Transactions[Date])
)
)
If the ALLEXCEPT was meant to be understood... It should remove all the filters from Transactions, and preserve the filter in column No.
But it wasn't and thus the result is the same:
So, maybe... ALLSELECTED?
Running total 3 =
CALCULATE(
SUM(Transactions[ESaldo]),
ALLSELECTED(Transactions[no])
)
Nope:
EDIT:
Here's the link to the pbix file:
https://drive.google.com/drive/folders/1W5cnLtIBD9REYbr3VdhIZqTcCtxz62hp?usp=sharing

Dear #nuno if i am not mistaken you might have already found the solution with this code
Running total 2 =
CALCULATE(
SUM(Transactions[ESaldo]),
FILTER(
ALLEXCEPT(Transactions, Transactions[no]),
Transactions[Date] <= MAX(Transactions[Date])
)
)
If you look closer you will see that from your prints runnig total 2 is actualy doing its calculation correctly by adding each invoice per row.
But the 166k that you see at the end is actually not suming the values above. It actually aplying the same measure to the entire table (another evaluation context) which is in fact correct to and is exacly the same as SUM(Transactions[ESaldo]) with no filters.
Nonetheless, given the nature of the example, this total is useless if your goal is to present as a table. you probably want the totals to be consistent.
Try the code below where i try to change the evaluation context inside calculate (sorry but the link above to pbix doesn't seem to work)
Running total 2 =
CALCULATE(
CALCULATE(
SUMX(
VALUES(Transactions[Date]),
CALCULATE(
SUM(Transactions[ESaldo])
)
)
)
FILTER(
ALLEXCEPT(Transactions, Transactions[no]),
Transactions[Date] <= MAX(Transactions[Date])
)
)
Does it work? it should change the context surrounding the date column and correct the total at the end

Related

Calculating Monthly Average over time with only Start and End Dates in Power BI

In order to track our average headcounts and turnover trends, I had created a report in Power BI that essentially expanded a table with employee start/end information to give a record for each associate every day they were employed at the company.
While this solution works, I end up with a table containing millions of rows which does not seem very efficient.
I have looked into other possible solutions in DAX, but have not found anything that will give me the exact same output. It seems like most solutions rely on taking the timing from the start date and end date of a month and then averaging them, which is not quite what I am trying to do.
I have attached a simplified pbix file that shows the desired output of what I am trying to get at. Any advice would be appreciated.
pbix example
Final code ended up looking like this:
All Employees = countrows ('Employees')
Terminations =
Calculate(
Countrows('Employees'),
USERELATIONSHIP('Employees'[End Date],'Calendar'[Date])
)
Active Employees =
AVERAGEX(
VALUES('Calendar'[Date]),
VAR CurrentDate = 'Calendar'[Date]
VAR HiredBeforeCurrentDate =
Filter(
ALL('Employees'[Start Date]),
'Employees'[Start Date] <= CurrentDate
)
VAR HiredAfterCurrentDate =
FILTER(
ALL('Employees'[End Date]),
OR('Employees'[End Date] >= CurrentDate, isblank('Employees'[End Date])
)
)
RETURN
CALCULATE(
Countrows('Employees'),
HiredBeforeCurrentDate,
HiredAfterCurrentDate,
ALL('Calendar')
))

Calculated measure not working with DAX filter

I have created NBRevenue Measure to get SUM of Revenue with two conditions as filter as per below screen shot, The issue is when i add [CurrentYear] then the data displayed in below screenshot(1) is wrong but when I hard code 2020 in screenshot(2) then data is correct.
I did check that the [CurrentYear] is whole number and the table 'AST DMNewLostMeasures'[Year] is too whole number.
Wrong Result- ScreenShot(1)
Correct Result - Screenshot(2)
I need to make this dynamic and hence please let me know what wrong I am doing or any other approach/suggestion would be great.
CurrentYear Formula
I think you should use a Year slicer rather than using a Measure reference inside your formula. That will make everything dynamic and result will be populated as per selected Year in the slicer. Your Revenue code should be as below-
NBRevenue =
CALCULATE(
SUM('AST DMNewLostMeasures'[Revenue]),
FILTER(
'AST DMNewLostMeasures',
'AST DMNewLostMeasures'[MeasureType] = "NewRevenue/Policies"
)
)
You can also change your CurrentYear measure as below-
CurrentYear =
CALCULATE(
MAX('AST DMNewLostMeasures'[Year]),
FILTER(
ALL('AST DMNewLostMeasures'),
'AST DMNewLostMeasures'[MeasureType] = "NewRevenue/Policies"
)
)
Assuming your CurrentYear measure returns what you expect, I'd recommend setting it as a variable rather than recalculating it for every row in your filter table:
NBRevenue =
VAR CurrYear = [CurrentYear]
RETURN
CALCULATE(
SUM('AST DMNewLostMeasures'[Revenue]),
FILTER(
'AST DMNewLostMeasures',
'AST DMNewLostMeasures'[MeasureType] = "NewRevenue/Policies"
&& 'AST DMNewLostMeasures'[Year] = CurrYear
)
)

DAX cannot compute a FILTER with "not equals" comparison

I have a single column with 400K rows, which contain random numbers.
The following DAX formula calculates immediately, but the inverse (just negating the equals condition) results in an Out Of Memory Exception (after the calculation runs for several minutes). Can you reproduce it and how to solve it? Thanks.
test_filter_1 = COUNTROWS(
FILTER(
table_1,
table_1[column1] = EARLIER(table_1[column1])
)
)
test_filter_1_negated = COUNTROWS(
FILTER(
table_1,
NOT(table_1[column1] = EARLIER(table_1[column1]))
)
)

Calculate cumulative sum of summarized table column

I having trouble calculating the cumulative sum of a column on PowerBI.
I have a big offer table and I want to run a pareto analysis on it. Following many tutorials, I created a SUMMARIZED table by offer and a sum of their sales. So the table definition is:
summary = SUMMARIZE(big_table; big_table[offer]; "offer sales"; sum(big_table[sales]))
Many of the forums and stackoverflow answers I found have direct me to the following formula for cumulative sum on column:
cum_sales =
CALCULATE(
sum([offer_sales]);
FILTER(
ALLSELECTED(summary);
summary[offer_sales] <= max( summary[offer_sales])
)
)
However the resulting table is not correct:
What I need is simply to have the offers ordered by sales descending and then add the current row's sales amount to the previous row's sales,
So I excepted numbers closer to:
1st row: 1.5M
2nd row: 2.1M
3rd row: 2.6M and so on
But (maybe) because of my data structure and (certainly) lack of knowledge on how PowerBI works, I'm not getting the right results...
Total Amount = SUM ( 'Fact'[Amount] )
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", [Total Amount]
)
VAR CurrentOfferAmount = [Total Amount]
VAR OffersLessThanCurrent =
FILTER (
OfferSum,
[amt] <= CurrentOfferAmount
)
RETURN
SUMX (
OffersLessThanCurrent,
[amt]
)
There's no need to pre-aggregate to a summary table. We can handle that as in the measure above.
This assumes a single fact table named 'Fact', and a table of distinct offers, 'Offer'.
Depending on what you're doing in terms of other filters on 'Offer', you may need to instead do as below:
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", CALCULATE ( [Total Amount], ALLEXCEPT ( 'Offer', 'Offer'[Offer] ) )
)
...
The rest of the measure would be the same.
The measure is fairly self-documenting in its VARs. The first VAR, OfferSum is a table with columns ('Offer'[Offer], [amt]). This will include all offers displayed in the current visual. CurrentOfferAmount is the amount for the offer on the current row/axis label of the visual. OffersLessThanCurrent takes OfferSum and filters it. Finally, we iterate OffersLessThanCurrent and add up the amounts.
Here's a sample:

DAX Last Year to Date

So I know this question has been asked a few times, and I've religiously looked over different approaches, however I still don't quite understand why I'm getting an incorrect result.
Case: I have Sales Data from ~2016 -> 2019 (up until the 2/18/2019) I'm have a Measure to show me the YTD, however I'm looking for a measure for Last Years to date(the 18th in this particular circumstance).
Right now, I have this:
Total Sales LYTD =
CALCULATE (
[Total Sales],
SAMEPERIODLASTYEAR (
FILTER (
VALUES ( Sales[Completed Date] ),
Sales[Completed Date] <= MAX ( Sales[Completed Date] )
)
)
)
The logic to me makes sense, but I'm sure I'm missing something, has it appears it's grabbing the ENTIRE total of 2018 when in reality i'm looking for 01/01/2018 -> 2/18/2018
This is going to be dynamically uploaded with new sales data
What am I missing? Thank you so much!
Not sure I understand your table setup so lets look at this scenario and hopefully it helps.
Suppose you have the data in two tables, Sales and Calendar, and there's a 1:* relationship between the calendar and the sales tables. Then I would write the measures like this:
SalesToDateThisYear =
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(Today())
)
and
SalesToDateLastYear =
var dateLastYear = Today() - 365
return
calculate(
Sum(Sales[Sales]);
Calendar[Year] = Year(dateLatsYear);
Calendar[Date] < dateLastYear
)
The two filter arguments are combined with a logic AND. So only dates from the first of last year to today's date last year will be included.
If you want to use the SamePeriod-function you can probably write something like this
SPLY =
calculate =
Sum(Sales[Sales]);
SamePeriodLastYear(
Filter(
Values(Calendar[Date]);
Calendar[Date] >= Date(year(today()); 1; 1) && Calendar[Date] < Today()
)
)
)
The SamePeriod-function takes a set of dates (this year) and converts them to dates last year.
Cheers