PowerBI rolling sum of sums - powerbi

I have 4 columns
Sales
Spend1
Spend2
Spend3
I want to calculate the rolling Net Sales
I know I can do this :
Running_Total =
CALCULATE (
SUM ( Revenue[Sales] ),
FILTER (
Targets[Date] <= EARLIER ( Targets[Date] )
)
)
What strategy can be used to calculate the rolling sum of the following formula :
Sales - Spend1 - Spend2 - Spend

Related

Cumulative total percentage in Power BI

I have a summary table in Power BI which shows how many days it takes for leads to convert to a sale. It has 2 columns, sum_convert (the amount of days in between lead creation date and converted date) and count_lead (the count of leads that have taken that amount of days to convert), both are numeric values. Here is an example of the data:
What I want, is a column next to count_lead that shows the running percentage total in the specific ascending order of sum_convert. Currently I've created a measure called lead_count which is the sum of count_lead. Then I've attempted to create the cumulative total with the following measure:
Cum_Lead = calculate([lead_count], FILTER(ALL(Convert_Count_Summary[Sum_Convert]), SUM(Convert_Count_Summary[count_lead]) <= [lead_count]))
This creates a cumulative total, but not in the specific sum_convert order, it's in the order of largest volume for count_lead. Any idea what I need to change so that it's in the order of sum_convert?
You could do this in Power Query using M:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)), type number)
Or as a calculated column using DAX:
Cumulative Count DAX =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
Edit:
Cumulative percentages in Power Query:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_Percent_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)) / List.Sum(#"Previous Step"[count_lead]), Percentage.Type)
Cumulative percentages calculated column in DAX:
Cumulative Count % DAX =
VAR _Numerator =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
VAR _Divisor =
SUM ( Convert_Count_Summary[count_lead] )
RETURN
DIVIDE (
_Numerator,
_Divisor
)

Calculating average from measures

This one should be quite easy, but I can't find a way to solve it...
A very simple setup: Three tables: products, sales and budgets.
Budget has four columns from source: ProductID,StartOfperiod,BudgetNumber.
Then I've added a calculated column, "Results" which calculate saleresults by product, month and year:
Results =
CALCULATE (
SUM ( sales[Numbers] );
FILTER ( sales; sales[ProductID] = Budget[ProductID] );
FILTER ( sales; MONTH ( sales[DateofSale] ) = MONTH ( Budget[StartOfperiod] ) );
FILTER ( sales; YEAR ( sales[DateofSale] ) = YEAR ( Budget[StartOfperiod] ) )
)
Works fine.
Then I added a measure for percentage of sales/budget:
SalesPercentage =
DIVIDE ( SUM ( Budget[Results] ); SUM ( Budget[BudgetNumber] ); 0 )
This also shows up fine.
I calculate for four products only, so when put in a matrix this gives a nice table showing sales results for these products.
But then I want to calculate the arithmetic average of SalesPercentage and put that value on i.e. a card. But how can I do that? All the necessary data are available in the budget table, but I can't find a way to just calculate the average of the SalesPercentage column, which is a measure. Anyone who knows how to?
Regards,
John Martin

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

Total Working Days Calculation in PowerBi

I have the that has some data related to employee start date and end date for specific task
How to calculate "Total Working days" using the above data in PowerBi
The expected ouput would be number of days this employee worked (according to date )
so for example
12/2/2018 12/3/2018 = 1 working Day
but if same record repeated this would remain one working day
Output from Formula
Screenshot of PBX File
You can calculate the working day in various ways, I'll give you two options (both are calculated columns):
WorkingDays1 = Tasks[EndTime] - Tasks[StartTime]
(Assuming it's a date column type), you can add .DATE to be sure behind the column)
OR
WorkingDays2 = DATEDIFF(Tasks[StartTime];Tasks[EndTime];DAY)
If you want to correct the amount, for the same day, you can use an IF statement:
WorkingDays3 =
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
If you wish to SUM the days, in a measure, you can SUM one of the generated columns above, OR you can create a measure which doesn't need such column: It iterates over the rows, and caculates the correct amount:
SUM WorkingDay =
SUMX (
Tasks;
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
)
Result:
Note: These calculations do not take the weekends, or holidays into account.
Edit:
If you only want a unique list of dates, with the working days, you can use something like this. It itarates over the start dates:
SUM WorkingDay2 =
SUMX (
VALUES ( Tasks[StartTime] );
IF (
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)
Edit 2:
If you want to create an unique list per day, you'll have to get rid of the time part in the datetime column. There are several ways to do this, but one option is to create a new column:
StartDate = Tasks[StartTime].[Date]
Then, you can adjust the measure by iteration over the dates, instead of the datetimes:
SUM WorkingDay3 =
SUMX (
VALUES ( Tasks[StartDate] );
IF (
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)

Power BI: Quarter over Quarter %Change

To start, here is the Power BI I am working with:
I want to calculate the %Change in Cost Quarter over Quarter.
As shown in the table above, I have the Cost Total for Q1, Q2, Q3, and Q4 in the Total Cost by Quarter Column, which I calculated using this formula:
Total Cost By Quarter =
IF (
[Quarters] = "Q1",
CALCULATE (
SUM ( CR_Months_ByMonth[Cost] ),
FILTER ( CR_Months_ByMonth, [Quarters] = "Q1" )
),
IF (
[Quarters] = "Q2",
CALCULATE (
SUM ( CR_Months_ByMonth[Cost] ),
FILTER ( CR_Months_ByMonth, [Quarters] = "Q2" )
),
IF (
[Quarters] = "Q3",
CALCULATE (
SUM ( CR_Months_ByMonth[Cost] ),
FILTER ( CR_Months_ByMonth, [Quarters] = "Q3" )
),
IF (
[Quarters] = "Q4",
CALCULATE (
SUM ( CR_Months_ByMonth[Cost] ),
FILTER ( CR_Months_ByMonth, [Quarters] = "Q4" )
)
)
)
)
)
However, I could not figure out how to calculate %Change between quarters using another Calculated column, due to the repeating values (multiple Q1s, Q2s, etc in [Total Cost By Quarter]).
So, I attempted to calculate the %Change using Measures.
I made a measure for the Q1 Cost, Q2 Cost, Q3 Cost, and Q4 Cost, using a formula like the one below:
Q1Sum =
CALCULATE (
SUM ( CR_Months_ByMonth[Cost] ),
FILTER ( CR_Months_ByMonth, [Quarters] = "Q1" )
)
I then made a new measure to calculate Q12%Change, Q23%Change, and Q34%Change, using a formula like the one below:
Q12%Change =
( DIVIDE ( [Q2Sum] - [Q1Sum], [Q1Sum] ) )
* 100
This is the result that I get using the calculated measures:
This structure does not yield good visuals and I am certain there is a simpler, more efficient way to accomplish Quarter over Quarter %Change.
This is my desired result:
As a final note, I do have a date table that looks like this:
THANK YOU!
[Total Cost by Quarter] should be as simple as SUM(CR[Cost]) if placed into a matrix that has quarters on the rows/columns.
The trickier part is referencing the previous quarter to get the percent change. It will look something like this:
% Change =
VAR PrevQtrCost = CALCULATE(SUM(CR[Cost]), PREVIOUSQUARTER(DateTable[Date]))
RETURN DIVIDE(SUM(CR[Cost]), PrevQtrCost) - 1
The VAR line might be a bit different depending on how exactly you have your DateTable related to the CR table.
Also take a look at this similar question: Power BI: Percent Change Formula
If you aren't linking on a date, then try something along these lines:
% Change =
VAR PrevQtr = MOD(MAX(DateTable[FiscalQuarterNumber]) - 2, 4) + 1
VAR PrevQtrCost = CALCULATE(SUM(CR[Cost]), DateTable[FiscalQuarterNumber] = PrevQtr)
RETURN DIVIDE(SUM(CR[Cost]), PrevQtrCost) - 1