Forum Discussion
contingency table with calculated rows
- Anonymous1 year ago
Hi,
You can use the following DAX to create a calculation table so that all rows are in the same column.Contingency table = VAR _tb1 = FILTER('Table','Table'[ID]IN{130,210}) VAR _tb2 = FILTER('Table','Table'[ID] = 550) RETURN UNION( SELECTCOLUMNS('Table',"ID",'Table'[ID],"Month",'Table'[Year-Month],"Amount",'Table'[Amount]), DISTINCT( SELECTCOLUMNS( _tb1,"ID","EBITDA","Month",'Table'[Year-Month],"Amount",CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210}))) ), DISTINCT( SELECTCOLUMNS( _tb1,"ID","EBITDA(%)","Month",'Table'[Year-Month],"Amount", DIVIDE( CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210})), CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),'Table'[ID]=130) ) ) ), DISTINCT( SELECTCOLUMNS( _tb2,"ID","EBIT","Month",'Table'[Year-Month],"Amount",CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210,550}))) ), DISTINCT( SELECTCOLUMNS( _tb2,"ID","EBIT(%)","Month",'Table'[Year-Month],"Amount", DIVIDE(CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210,550})), CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),'Table'[ID]=130) ) ) ) )Then create a measure to display the values for the different rows.
Value = IF(MAX('Contingency table'[ID]) IN {"EBIT(%)","EBITDA(%)"}, FORMAT(SUM('Contingency table'[Amount]),"#.0%") ,SUM('Contingency table'[Amount]))
The final result is as follows, hopefully it will meet your needs.
Please see the attached pbix for reference.
Best Regards,
Dengliang Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi,
You can use the following DAX to create a calculation table so that all rows are in the same column.
Contingency table =
VAR _tb1 = FILTER('Table','Table'[ID]IN{130,210})
VAR _tb2 = FILTER('Table','Table'[ID] = 550)
RETURN
UNION(
SELECTCOLUMNS('Table',"ID",'Table'[ID],"Month",'Table'[Year-Month],"Amount",'Table'[Amount]),
DISTINCT(
SELECTCOLUMNS(
_tb1,"ID","EBITDA","Month",'Table'[Year-Month],"Amount",CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210})))
),
DISTINCT(
SELECTCOLUMNS(
_tb1,"ID","EBITDA(%)","Month",'Table'[Year-Month],"Amount",
DIVIDE(
CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210})),
CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),'Table'[ID]=130)
)
)
),
DISTINCT(
SELECTCOLUMNS(
_tb2,"ID","EBIT","Month",'Table'[Year-Month],"Amount",CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210,550})))
),
DISTINCT(
SELECTCOLUMNS(
_tb2,"ID","EBIT(%)","Month",'Table'[Year-Month],"Amount",
DIVIDE(CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),KEEPFILTERS('Table'[ID] IN {130,210,550})),
CALCULATE(SUM('Table'[Amount]),ALLEXCEPT('Table','Table'[Year-Month]),'Table'[ID]=130)
)
)
)
)
Then create a measure to display the values for the different rows.
Value =
IF(MAX('Contingency table'[ID]) IN {"EBIT(%)","EBITDA(%)"},
FORMAT(SUM('Contingency table'[Amount]),"#.0%")
,SUM('Contingency table'[Amount]))
The final result is as follows, hopefully it will meet your needs.
Please see the attached pbix for reference.
Best Regards,
Dengliang Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.