Forum Discussion

Gandhary80's avatar
Gandhary80
Frequent Visitor
1 year ago
Solved

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?

 

 

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

 

  • Anonymous's avatar
    Anonymous
    1 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.



     

1 Reply

  • Anonymous's avatar
    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)
                )
            )
            )            
        )

     

    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.