My table has a column that calculates the cumulative total year-to-date (YTD). The column is Balance BRL. It works ok using the DAX function DATESYTD.
The column Balance BRL is perfectly achieved using the DAX below:
Balance BRL =
CALCULATE (
'Fact_balance_cash'[realized_application],
DATESYTD('Calendar'[Date])
)
+
CALCULATE (
'Fact_balance_cash'[realized_investiments],
DATESYTD('Calendar'[Date])
)
+
CALCULATE (
'Fact_balance_cash'[realized_balance],
DATESYTD ('Calendar'[Date])
)
+
CALCULATE (
[Forecast_R],
DATESYTD ( 'Calendar'[Date] )
)
I was asked to continue to accumulate the values over 2023. That is not compatible with the formula DATESYTD.
I read this article from daxpatterns.com: Link Cumulative Total
I tried to implement it. the result is -R$ 148.017.749,0527 which is indeed the final balance. But it seems the date does not take effect to properly calculate it by month.
My current measure is the one below. I am fixing the date period until the end of January in 2023 just to test:
Balance BRL =
var period_begin = DATE(2022,1,1)
var period_end = DATE(2023,1,31)
return
CALCULATE (
'Fact_balance_cash'[realized_application],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
'Fact_balance_cash'[realized_investiments],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
'Fact_balance_cash'[realized_balance],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
[Forecast_R],
'Calendar'[Date]<=period_end
)
How to solve this cumulative total that continues to calculate from 2022 until 2023?
Is this what you want (New Column):
New =
var period_end = MAX('Calendar'[Date])
return
CALCULATE (
'Fact_balance_cash'[__auto_application_actutal],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
'Fact_balance_cash'[__investment_actual],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
'Fact_balance_cash'[__balance_actual],
'Calendar'[Date]<=period_end
)
+
CALCULATE (
[__Forecast_R],
'Calendar'[Date]<=period_end
)
Can you try something like-
CALCULATE (
SUM('Fact_balance_cash'[realized_application]),
FILTER(
ALL('Calendar'),
'Calendar'[Date]>=period_begin
&& 'Calendar'[Date]<=period_end
)
)
There is also a function like DATESBETWEEN you can check.
VAR startDate =
CALCULATE(
MIN(Calendar[Date])
,AllSELECTED(Calendar)
)
VAR endDate = MAX(Calendar[Date])
RETURN
CALCULATE(
[realized_application]
+ [realized_investiments]
+ [realized_balance]
+ [Forecast_R]
,'Calendar'[Date]>=startDate && 'Calendar'[Date]<=endDate
)
Related
I am trying to create new measures that will only show the data value of the Last day every month and another measure will show the data value of every week Saturday in Power BI.
Many thanks in Advance.
The data is continuing and the next series of data will appended data wise
Output:
Need to create a measure which will show only last day of the Week value ( Example- Saturday is my Last of the Week and Count numbers I need to see as output)
Similarly for Month - Last day of the Month I need to see the Numbers of each service.
Do you have Calendar table mark as "Date Table", which is connected by relationship to your Table? If so, then you can use ENDOFMONTH ( 'Calendar'[issue_date] ) to get lastDateOfMonth
then simply:
ShowValueAtLastDayOfMonth =
calculate( sum(Table[Total sale]),
KEEPFILTERS(
filter(ALL('Calendar'[issue_date]),
'Calendar'[issue_date] = ENDOFMONTH ( 'Calendar'[issue_date] )
)
)
)
ShowOnlyOnSaturday =
calculate(sum(Table[Total sale]), KEEPFILTERS( FLITER(ALL('Calendar[issue_date]),
WEEKDAY ( SELECTEDVALUE('Calendar'[issue_date])) = 7 )))
Without a "Date Table" we need to replace 'Calendar'[issue_date] with your Table[Date] and ENDOFMONTH with:
VAR LastDateVisible =
CALCULATE ( MAX ( 'Table'[Date] ) )
VAR LastYearVisible =
YEAR ( LastDateVisible )
VAR LastMonthVisible =
MONTH ( LastDateVisible )
VAR DaysInMonth =
FILTER (
ALL ( 'Table'[Date] ),
YEAR ( 'Table'[Date] ) = LastYearVisible
&& MONTH ( 'Table'[Date] ) = LastMonthVisible
)
VAR LastDayInMonth =
MAXX (
DaysInMonth,
'Table'[Date]
)
VAR Result =
CALCULATETABLE (
VALUES ( 'Table'[Date] ),
'Table'[Date] = LastDayInMonth
)
RETURN
Result
I have this table :
And I want to calculate for each day and for each group 1and2 % which is
(Attribute1/Attribute2)*100
But I don't want to include 1and2 % in Group Subtotal neither on total
I created a new table :
Table3 = UNION(DISTINCT('Table'[Attributes]);{{"1and2 %"}})
And i made this mesure to calculate 1and2 %:
Measure =
SUMX (
DISTINCT ( 'Table3'[Attributes] );
SWITCH (
'Table3'[Attributes];
"1and2 %"; DIVIDE (
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute1" );
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute2" );
0
) * 100;
VAR a = 'Table3'[Attributes]
RETURN
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = a )
)
)
And this is the result I'm getting :
I want to exclude 1and2 % (and every Attribute containing % in the future) from group subtotal and the total.
I'm really new to PowerBI and not really familiar with Excel-like formulas.
Can anyone please help me up with that?
Regards
Someone from PowerBI forum gave me this solution and it's exactly what im looking for:
Create another mesure using the existing :
Measure 2 =
VAR __Table =
ADDCOLUMNS(
SUMMARIZE(
'Table 3',
[Group],
[Attribute],
"__Measure",[Measure]
),
"__IncludeInTotals",SEARCH("%",[Attribute],,-1)
)
RETURN
IF(
HASONEVALUE('Table 3'[Attribute]),
[Measure],
SUMX(FILTER(__Table,[__IncludeInTotals] = -1),[__Measure])
)
I think you can get what you want if you only calculate the 1and2 % part when that's the only thing in the filter context and do a normal sum otherwise.
Measure =
IF (
SELECTEDVALUE ( 'Table3'[Attributes] ) = "1and2 %";
DIVIDE (
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute1" );
CALCULATE ( SUM ( 'Table'[Value] ); 'Table'[Attributes] = "Attribute2" );
0
) * 100;
SUM ( 'Table'[Value] )
)
For the subtotals and grand total, SELECTEDVALUE returns blank so the condition fails and you just get the sum.
I want to display percentage increase or decrease in total for each month as I select each month i.e when I click on FEB, it should tell me whether there was a percentage increase/decrease in expenses compared to JAN.
I have tried different codes but keep getting an error message.
Here is a DAX CODE I tried:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
population[MONTH]
= ( EARLIER ( population[MONTH] ) - 1 )
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
I want a new column created to display the percentage increase or decrease from a month to its previous month.
Here is a screenshot of the excel document:
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
DATEDIFF(population[MONTH], EARLIER ( population[MONTH] ),MONTH) = 1
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)
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
After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: