Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Gandhary80
Frequent Visitor

contingency table with calculated rows

Hello, 

 

I have the data source as you can see on the picture on the left side (you can find it also in the table inserted below. My goal is to have the contingency table - the same like you can see on the green background on the picture. 

 

My questions to this problem are:

1) How to create the contingency table with rows made up of "ID"

2) How to calculate the values EBITDA, EBITDA (%), EBIT and EBIT (%)

3) How to have all these rows in just one contingenc table?

 

Gandhary80_1-1730377475849.png

 

DateIDAmountType
20.04.202313010300EBITDA
10.05.202313011800EBITDA
13.04.20231308900EBITDA
10.05.2023210-5400EBITDA
01.04.2023210-6500EBITDA
13.04.2023210-4900EBITDA
24.05.2023210-300EBITDA
10.05.2023550-2500EBIT
24.05.2023550-2300EBIT
13.04.2023550-1900EBIT

 

Thank you in advance for your suggestion of solution. It will help a lot 🙂

 

František

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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)
            )
        )
        )            
    )

vdengllimsft_0-1730444254597.png

 

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.

vdengllimsft_1-1730444673927.png


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.



 

View solution in original post

1 REPLY 1
Anonymous
Not applicable

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)
            )
        )
        )            
    )

vdengllimsft_0-1730444254597.png

 

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.

vdengllimsft_1-1730444673927.png


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.



 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors