Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Calculation using a previous row within a hierarchy

I'm wanting to do a calculation to create a column or calculated measure using 2 values on differing rows. My data is a flat file (excel) and the table shown below contains a hierachy that I've creat...
  • v-yingjl's avatar
    5 years ago

    Hi Anonymous ,

    Based on your picture, I think it is the part of your data I will just focus on the forecast result ignoring the sum value of other two fields [Retention] and [Enrolled].

    Also, since the [Retention] is a measure, I have created a simple sum measure to imitate it in my sample file:

     

    Retention = SUM('Table'[value])

     

    To get the previous row value in this case to calculate the forecast value, you can create this measure:

     

    Forecast =
    VAR tab =
        SUMMARIZE (
            'Table',
            'Table'[Department],
            'Table'[Class],
            'Table'[Block],
            'Table'[Enrolled],
            "prows",
                VAR _pre =
                    CALCULATE (
                        MAXX ( 'Table', [Retention] ),
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Department] IN DISTINCT ( 'Table'[Department] )
                                && 'Table'[Class] IN DISTINCT ( 'Table'[Class] )
                                && 'Table'[Block]
                                    = CALCULATE (
                                        MAX ( 'Table'[Block] ),
                                        FILTER (
                                            ALL ( 'Table' ),
                                            'Table'[Department] IN DISTINCT ( 'Table'[Department] )
                                                && 'Table'[Class] IN DISTINCT ( 'Table'[Class] )
                                                && 'Table'[Block] < MAX ( 'Table'[Block] )
                                        )
                                    )
                        )
                    )
                RETURN
                    IF ( ISBLANK ( _pre ), 0, _pre )
        )
    RETURN
        SUMX ( tab, [prows] * [Enrolled] )
    

     

    Attached my sample file in the below, hopes to help you.

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.