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.
Related
I have a table with the following columns:
Industry table
Industry_ID Score
1 2
1 3
2 2
2 4
3 0
4 2
I need to calculate the average of each industry and then the average of those averages.
Like avg of scores of
1=(2+3)/2 =>2.5
2=(2+4)/2 =>3
3=0/1 => 0
4=2/1 => 2
Then average of these averages, i.e (2.5+3+0+2)/4 => 1.85
The tables are in direct query so please consider that. Any help is appreciated. Thank you
For creating the average of distinct values, create a calculated column as:
Average =
var no_ID = 'Table'[Industry_ID]
Return
AVERAGEX(
FILTER(ALL('Table'), 'Table'[Industry_ID] = no_ID),
'Table'[Score]
)
This will give you a column having average of distinct Industry_ID.
For creating an average of averages, create a measure as:
Measure = AVERAGEX(SUMMARIZE('Table', 'Table'[Industry_ID], 'Table'[Average]), 'Table'[Average])
Final Output-
Here are 2 ways to achieve that:
Just switch between Individual and Overall Average variables in the RETURN part, also store this code CALCULATE ( COUNTROWS ( Industry ) ) in a separate measure so that it can be re-used in various places without making the code verbose
Industry Average =
VAR AllIndustryAverages =
AVERAGEX (
ALL ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
VAR IndividualAverages =
AVERAGEX (
VALUES ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
RETURN
IndividualAverages
Industry Average 2 =
VAR VisibleIndustries =
VALUES ( Industry[IndustryID] )
VAR AllIndustryAverages =
ADDCOLUMNS (
ALL ( Industry[IndustryID] ),
"Average",
VAR CurrentIndustryTotalScore = [Total Score]
VAR IndustryCount =
CALCULATE ( COUNTROWS ( Industry ) )
RETURN
DIVIDE ( CurrentIndustryTotalScore, IndustryCount )
)
VAR IndividualAverages =
AVERAGEX (
FILTER ( AllIndustryAverages, Industry[IndustryID] IN VisibleIndustries ),
[Average]
)
VAR OverallAverage =
AVERAGEX ( AllIndustryAverages, [Average] )
RETURN
IndividualAverages
Trying to add a measure in PowerBI that calculates the rolling 12-month sum of sales and the measure works fine up until the most recent 12 months worth of data. Not sure what's causing this error. Below is the data and code in PowerBI I'm using.
TTM MRR =
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
FILTER (
ALL ( MRR[CLOSE_MONTH] ),
AND (
MRR[CLOSE_MONTH] <= MAX ( MRR[CLOSE_MONTH] ),
DATEADD ( MRR[CLOSE_MONTH], 1, YEAR ) > MAX ( MRR[CLOSE_MONTH] )
)
)
)
Data:
[Excel Data]
It might be easier with DATESINPERIOD.
TTM MRR =
VAR PeriodEnd = MAX ( MRR[CLOSE_MONTH] )
RETURN
CALCULATE (
SUM ( MRR[MONTHLY_REV] ),
DATESINPERIOD ( MRR[CLOSE_MONTH], PeriodEnd, -12, MONTH )
)
enter image description hereI have daily values of closing balances of balance sheet. I want to have a measure which calculates the percentage change between two dates. The issue is that on weekdays the values are empty. So I have written the following measure which works when there are no filters, but when I place a date filter, the values of the measure become zeros.
Change_BS %(daily) =
IF (
SUM ( 'DB Daily_BS'[Value] ) = 0,
0,
IF (
SUM ( 'Date'[Weekday] ) = 1,
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) )
),
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) )
)
)
)
de a new Column called BS_Value which fills in the gaps for the weekends.
BS_Value =
var wday = MOD(WEEKDAY(Balance[Date]);7)
var daysBack = IF(wday < 2; wday + 1)
return CALCULATE(SUM(Balance[Value]);Balance[Date] = EARLIER(Balance[Date]) - daysBack)
Based on this column I made BS-Change what is the increment/decrement form previous:
BS_Change = Balance[BS_Value] - CALCULATE(SUM(Balance[BS_Value]);Balance[Date] = EARLIER(Balance[Date]) - 1)
End result:
You can now make measures or other columns based on those values. I need to say a more dynamic solution is possible where we would go for a lookup of the last value not zero. I did not do this because I can imagine the balance is zero and it would then skip those values.
Situation:
I have a column in my table with values representing weeks of the year.
Each week number has their respective total counts of purchases on another column. When I use a matrix visual and put that specific column in the Columns section it separates them distinctively which is what I want. How can get the % change from one week to another?
Table looks like this:
Objective:
Create a measure that can divides column 2 by column 1 to get the % change.
Layout of Matrix:
Ideally I would like to have a third column to calculate the values in column 6 by the ones in column 5.
OK, here is one solution:
Delta :=
VAR Week5 =
CALCULATE ( SUM ( 'Table'[Total] ), FILTER ( 'Table', 'Table'[Weeks] = 5 ) )
VAR Week6 =
CALCULATE ( SUM ( 'Table'[Total] ), FILTER ( 'Table', 'Table'[Weeks] = 6 ) )
RETURN
IF (
SUM ( 'Table'[Total] ) = SUMX ( ALL ( 'Table' ), 'Table'[Total] )
|| SUM ( 'Table'[Total] )
= SUMX (
FILTER ( ALL ( 'Table' ), 'Table'[Cohort] = MAX ( 'Table'[Cohort] ) ),
'Table'[Total]
),
100*DIVIDE ( Week6 - Week5, Week5 ),
BLANK ()
)
I tested it and it works:
BUT because the way your data is structured, it makes it very difficult (for me) to make this pretty. But hey, it works!! ;) I wasn't sure which direction you needed the delta but that is a simple change in the measure from this:
100*DIVIDE ( Week6 - Week5, Week5 )
To this:
100*DIVIDE ( Week5 - Week6, Week6 )
Hope this help!
Problem:
I need a calculated measure in DAX that sums the Value column for the last 6 sprints. I am basing the last 6 sprints on the DimSprintEndDateKey in descending order.
Table structure in PowerBI
The DAX that I am using:
CALCULATE (
SUM ( factSprint[Value] ),
FILTER (
ALL ( factSprint ),
COUNTROWS (
topn(6,
FILTER (
factSprint,
EARLIEST( RELATED ( dimSprint[DimSprintEndDateKey] ) )
> RELATED ( dimSprint[DimSprintEndDateKey] )
),RELATED ( dimSprint[DimSprintEndDateKey] ), DESC
)
)
)
)
I am assuming that the relationship on your tables is between 'dimSprint'[dimSprintKey] and 'FactSprint'[dimSprintKey].
That being the case, this measure could work for you.
Total Value Last Six Sprints =
VAR endDateSprint =
LOOKUPVALUE (
'dimSprint'[dimSprintEndDateKey],
'dimSprint'[dimSprintKey], SELECTEDVALUE ( 'FactSprint'[dimSprintKey] )
)
VAR dimTableFiltered =
FILTER ( 'dimSprint', 'dimSprint'[dimSprintEndDateKey] <= endDateSprint )
RETURN
CALCULATE (
SUM ( 'FactSprint'[Value] ),
ALL ( 'FactSprint' ),
TOPN ( 6, dimTableFiltered, [dimSprintEndDateKey], DESC )
)
Use it on a matrix visual (or pivottable). Be sure to put 'FactSprint'[dimSprintKey] or 'FactSprint'[SprintPK] on Rows and [Total Value Last Six Sprints] on Values.