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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply

Dynamic comparison among columns or rows with percentage

Hi everyone,

 

I have database as the below formats. 

IDweek 1week 2week 3week 4
12468
21357
314710
413121110

 

Or:

IDweekQuantity
1week 12
1week 24
1week 36
1week 48
2week 11
2week 23
2week 35
2week 47
3week 11
3week 24
3week 37
3week 410
4week 113
4week 212
4week 311
4week 410

 

Both rows and columns will be increased, which means more ID and week number will be added by time.

 

I need to compare the growth or decrease of each ID over weeks, for example week 2 compares to week 1:

ID1:

- week 1 make 2

- week 2 make 4

So in week 2, ID1 make: (4 - 2)/2 * 100% = 100% -> The % growth = 100%

 

More complicated, compare 2 consecutive weeks, for example (week 3 + week 4) compare to (week 2 + week 1)

 

Any suggestion is highly appreciated!

 

Many thanks in advance!

 

Regards,

Cindy

 

1 ACCEPTED SOLUTION
v-haibl-msft
Employee
Employee

@BusinessAnalyst

 

If you have more IDs and weeks, firstly add an index column as ankitpatira said. I’ve uploaded my .pbix file here for reference.

For the growth or decrease of each ID over week, we can create a column with following formula.

Growth_Decrease = 
VAR LastWeekIndex =
    IF (
        Table1[Index]
            = CALCULATE ( MIN ( Table1[Index] ), ALLEXCEPT ( Table1, Table1[ID] ) ),
        Table1[Index],
        Table1[Index] - 1
    )
VAR LastWeekQuantity =
    LOOKUPVALUE ( Table1[Quantity], Table1[Index], LastWeekIndex )
RETURN
    ( ( Table1[Quantity] - LastWeekQuantity )
        / LastWeekQuantity )

For the growth or decrease of 2 consecutive weeks, we can create some columns with following formulas.

Consecutive_Week = 
IF (
    DIVIDE ( Table1[Index], 2 ) > INT ( DIVIDE ( Table1[Index], 2 ) ),
    ( Table1[Index] + 1 )
        / 2,
    Table1[Index] / 2
)
Consecutive_WeekName =
"Consecutive Week "
    & RANKX (
        FILTER ( Table1, EARLIER ( Table1[ID] ) = Table1[ID] ),
        Table1[Consecutive_Week],
        ,
        ASC,
        DENSE
)
Growth_Decrease_2 = 
VAR LastWeekIndex =
    IF (
        Table1[Consecutive_Week]
            = CALCULATE ( MIN ( Table1[Consecutive_Week] ), ALLEXCEPT ( Table1, Table1[ID] ) ),
        Table1[Consecutive_Week],
        Table1[Consecutive_Week] - 1
    )
VAR LastWeekQuantity =
    CALCULATE (
        SUM ( Table1[Quantity] ),
        FILTER ( ALL ( Table1 ), Table1[Consecutive_Week] = LastWeekIndex )
    )
VAR ThisWeekQuantity =
    CALCULATE (
        SUM ( Table1[Quantity] ),
        ALLEXCEPT ( Table1, Table1[Consecutive_Week] )
    )
RETURN
    ( ( ThisWeekQuantity - LastWeekQuantity )
        / LastWeekQuantity )

Dynamic comparison among columns or rows with percentage_1.jpg

 

Best Regards,

Herbert

View solution in original post

2 REPLIES 2
v-haibl-msft
Employee
Employee

@BusinessAnalyst

 

If you have more IDs and weeks, firstly add an index column as ankitpatira said. I’ve uploaded my .pbix file here for reference.

For the growth or decrease of each ID over week, we can create a column with following formula.

Growth_Decrease = 
VAR LastWeekIndex =
    IF (
        Table1[Index]
            = CALCULATE ( MIN ( Table1[Index] ), ALLEXCEPT ( Table1, Table1[ID] ) ),
        Table1[Index],
        Table1[Index] - 1
    )
VAR LastWeekQuantity =
    LOOKUPVALUE ( Table1[Quantity], Table1[Index], LastWeekIndex )
RETURN
    ( ( Table1[Quantity] - LastWeekQuantity )
        / LastWeekQuantity )

For the growth or decrease of 2 consecutive weeks, we can create some columns with following formulas.

Consecutive_Week = 
IF (
    DIVIDE ( Table1[Index], 2 ) > INT ( DIVIDE ( Table1[Index], 2 ) ),
    ( Table1[Index] + 1 )
        / 2,
    Table1[Index] / 2
)
Consecutive_WeekName =
"Consecutive Week "
    & RANKX (
        FILTER ( Table1, EARLIER ( Table1[ID] ) = Table1[ID] ),
        Table1[Consecutive_Week],
        ,
        ASC,
        DENSE
)
Growth_Decrease_2 = 
VAR LastWeekIndex =
    IF (
        Table1[Consecutive_Week]
            = CALCULATE ( MIN ( Table1[Consecutive_Week] ), ALLEXCEPT ( Table1, Table1[ID] ) ),
        Table1[Consecutive_Week],
        Table1[Consecutive_Week] - 1
    )
VAR LastWeekQuantity =
    CALCULATE (
        SUM ( Table1[Quantity] ),
        FILTER ( ALL ( Table1 ), Table1[Consecutive_Week] = LastWeekIndex )
    )
VAR ThisWeekQuantity =
    CALCULATE (
        SUM ( Table1[Quantity] ),
        ALLEXCEPT ( Table1, Table1[Consecutive_Week] )
    )
RETURN
    ( ( ThisWeekQuantity - LastWeekQuantity )
        / LastWeekQuantity )

Dynamic comparison among columns or rows with percentage_1.jpg

 

Best Regards,

Herbert

ankitpatira
Community Champion
Community Champion

@BusinessAnalyst provided you have second screenshot data you can create calculated column using below DAX. Replace 'sam' with your table name. Before creating calculated column create Index column starting from 0 via query editor.

 

Capture.PNG

 

Column = sam[Quantity] - IF(
OR(sam[Index] = 0 , sam[Index] = 4),
sam[Quantity],
LOOKUPVALUE(
sam[Quantity],
'sam'[Index],
'sam'[Index]-1)
)

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.