DAX code for cumulative sum (in a column) - powerbi

I am working on a project, where a column needs cumulative summing. How do I find the cumulative sum for a column and store in another column.?
I expect the output as shown:

Assuming you have a date table and are using that to calculate you running total; you could follow the DAX pattern provided below or just amend it to fit your needs:
CumSum :=
VAR MaxDate =
MAX ( 'Date'[Date] )
RETURN
CALCULATE ( SUM ( 'Table'[A] ), 'Date'[Date] <= Maxdate, ALL ( date ) )

Related

SUMMARIZE or SUM values based on Date fields in another table

I am trying to do a burndown graph on PowerBI.
My data is a list of tasks. Each task has a numerical value (EFFORT) assigned to it and there is a total amount of effort for any given month (sum of all EFFORT). As tasks as set to DONE, the ongoing effort should be deducted from a running total and that value used to plot a graph. I have 3 columns
I would like to have measure to calculate EFFORT REMAINING for each date, i.e.
EFFORT REMAINING = TOTAL EFFORT - (EFFORT WHEN TASKS ARE DONE FOR EACH DAY)
For example,
I did get the consecutive dates displaying:
Burndown = CALENDAR(DATE(2022,7,1),DATE(2022,7,31))
and also the total effort (starting value)
TOTAL EFFORT = SUM(Issues[EFFORT])
Now for each date in table, I need to minus the accumulating total of EFFORT when the status is set to DONE
EFFORT REMAINING = Burndown[TOTAL EFFORT]-SUM(Issues[EFFORT]="DONE" ....
Im stuck after this last point. Can anyone help, please?
you are so close to the answer ). Convert SUM(Issues[EFFORT]="DONE" to:
CALCULATE(
SUM(Issues[EFFORT])
, SUM(Issues[Status]="DONE"
)
Have a nice day.
Please try this measure:
Please ensure that (1-Many) relationship is created between Burndown [Date] and Issues[ISSUE_CREATED] columns.
EFFORT REMAINING =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( Issues, Burndown[Date] ),
"Total Effort", CALCULATE ( SUM ( Issues[EFFORT] ) ),
"Tasks Completed", CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" ),
"Effort Remaining",
CALCULATE ( SUM ( Issues[EFFORT] ) )
- CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" )
)
VAR Result =
SUMX ( TblSummary, [Effort Remaining] )
RETURN
Result
After that, you can create a clustered column chart, and put [Date field] on calendar table on X_axis and put 'EFFORT REMAINING' measure on Y_axis(Value axis) to see the result.
I hope It solves your problem.
Bonus Info:
If you want to see your Summary table, create a "New Table" and paste this code:
Summary_Table =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( Issues, Burndown[Date] ),
"Total Effort", CALCULATE ( SUM ( Issues[EFFORT] ) ),
"Tasks Completed", CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" ),
"Effort Remaining",
CALCULATE ( SUM ( Issues[EFFORT] ) )
- CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" )
)
VAR Result =
SUMX ( TblSummary, [Effort Remaining] )
RETURN
TblSummary
The result It produces:
Note: I have limited access to your data sets as you shared above. The result will be exact with your full dataset.

How to create a cumulative sum over two columns with comlementary Date Range in Power BI

I'm trying to build a line Chart to show the cumulative sum of the Forcast. I have two Tables, one for the Actuals and one for the forcast, both linked to a Date table. The Chart should show the cumulative sum of the actuals till current month and from next month on those from the Forcast.
I've created so far the following measures to get the Forcast Chart:
Chart_Forecast not cumulated =
VAR Actual_Hrs_not_cumulated = CALCULATE([Total Actuals],FILTER(IN_ACTUALS, IN_ACTUALS[Date] <= MAX(CurrentMonth[CurrentMonthParameter])))
VAR Forecast_not_cumulated = CALCULATE([Total Forecast],FILTER(IN_Forecast, IN_Forecast[Date] > MAX(CurrentMonth[CurrentMonthParameter])))
RETURN
IF((SELECTEDVALUE('LT_Reporting Calendar'[Date]) <= MAX(CurrentMonth[CurrentMonthParameter])),Actual_Hrs_not_cummulated, Forecast_not_Cumulated)
This one gives me the line chart of the non cumulated Forcast, and it works.
But as soon as I want to build the cumulative sum on the measure above according to the measure below, I get only the cumulative sum till current month and the future is omitted. I think I have a filter issue.
I've tried many methods of building a cumulative sum, always getting the same result.
Chart_Forcast Cumulated =
CALCULATE(
[Chart_Forecast not cumulated],
FILTER(
ALL('LT_Reporting Calendar'[Date]),
'LT_Reporting Calendar'[Date] <= MAX ('LT_Reporting Calendar'[Date])
)
)
Input:
Result: The Chart
Does any body have an Idea on this?
Many Thanks in advance
I'd suggest writing it as a sum like this:
Cumulative =
VAR ParameterDate = SELECTEDVALUE ( CurrentMonth[CurrentMonthParameter] )
VAR AxisDate = SELECTEDVALUE ( 'LT_Reporting Calendar'[Date] )
RETURN
CALCULATE (
[Total Actuals],
'LT_Reporting Calendar'[Date] <= AxisDate,
'LT_Reporting Calendar'[Date] <= ParameterDate
) +
CALCULATE (
[Total Forecast],
'LT_Reporting Calendar'[Date] <= AxisDate,
'LT_Reporting Calendar'[Date] > ParameterDate
)

Return date based on a condition in Power BI using DAX

hope you can help me.
I need to calculate in Power BI a date difference between today() and a certain date based on a condition.
I have a calendar table with the date (calendario[fecha]) related to a fact table ASID to predict column ASID[amount] and a measeure [Estimado] that gives me the linear regression
Estimado =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( 'calendario'[fecha] ),
"Known[X]", calendario[fecha],
"Known[Y]", [ASID]
),
AND (
NOT ( ISBLANK ( Known[X] ) ),
NOT ( ISBLANK ( Known[Y] ) )
)
)
VAR Count_Items =
COUNTROWS ( Known )
VAR Sum_X =
SUMX ( Known, Known[X] )
VAR Sum_X2 =
SUMX ( Known, Known[X] ^ 2 )
VAR Sum_Y =
SUMX ( Known, Known[Y] )
VAR Sum_XY =
SUMX ( Known, Known[X] * Known[Y] )
VAR Average_X =
AVERAGEX ( Known, Known[X] )
VAR Average_Y =
AVERAGEX ( Known, Known[Y] )
VAR Slope =
DIVIDE (
Count_Items * Sum_XY - Sum_X * Sum_Y,
Count_Items * Sum_X2 - Sum_X ^ 2
)
VAR Intercept =
Average_Y - Slope * Average_X
RETURN
ROUND(
SUMX (
DISTINCT ( calendario[fecha] ),
Intercept + Slope * calendario[fecha]
),0)
My visualization matrix has 3 columns: calendario[fecha], it's real value [ASID] and the estimated measure [Estimado].
I have a limit of 1105 for that ASID.
I can see that at a future day, let's say a month from now 03/12/2020, the estimated reaches a value of 1105 (after scrolling all the matrix), so I need a way to capture that day and be able to calculate 03/12/2020 - today() and display somewhere: "30 days left"
Raihan: I could use the datediff as you suggested
matrix
Is there a way to capture just the 231 value?
DAX is now: if([Estimado]>1105, DATEDIFF(TODAY(),LASTDATE(calendario[fecha]),DAY),0)
As you didn't provide the sample dataset and you didn't tell about your measure formula I just consider a sample dataset on my own to simulate your problem.
Consider following screenshot with data and calculated columns.
Here the DaysFromToday calculate the Day difference from Today to for every column if the corresponding value in 'SomeValue' field reached a certain number. SomeValue is also calculated field that you can replace with your own calculation.
To get the single value from DaysFromToday you can have a measure which will give you MAX or MIN (of course some others functions if you need) of the column values like following screenshot -
Yellow highlighted is the DAX way of mentioning a field name with the table name that you are missing in your formula.
Lastly the MAX or MIN measure can be displayed in the report like following -
it will be better if you can provide sample dataset and sample answer that you want.

Is there Dax code to take the difference between 2 rows from a summarized table

I need to be able to get the difference between 2 successive rows in a Summarized table, by rank, so that I can then get the average of the differences of each row. And I can't create a new table as I need this DAX query to be filterable
I've been able to get this far, but do not know how to add a difference column that will show the DSOValue difference between rows 1-2, 2-3, 3-4 ...
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[Date],
"DSOValue", DIVIDE ( SUM ( 'Table1'[AR] ) * 91.5, SUM ( 'Table1'[Sales] ), 0 )
),
"Rank", RANKX (
Table1,
CALCULATE (
COUNTROWS ( Table1),
FILTER ( Table1, Table1[Date] <= EARLIER ( Table1[Date] ) )
),,ASC,DENSE)
)
I've tried embedding this code within another ADDCOLUMNS function, but it won't let me CALCULATE and FILTER on my created columns (DSOValue and RANK)
you can use the following:
Diff = 'Table1'[ DSOValue ] - LOOKUPVALUE('Table1'[ DSOValue ]; 'Table1'[Date ];CALCULATE( MAX('Table1'[Date ]);FILTER('Table1';'Table1'[Date ]<EARLIER('Table1'[Date ]))))
Note: You cannot use <= here, it will pick its own date and all will be null. It would also be easier to add an index column, when you have this you can use Lookupvalue with Index -1

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