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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Billing Cycle Calculation (Rank, Count) field

We have order transaction data table, in which we want to add a calculated field for Billing cycle. This Billing cycle will be used as a slicer in the model.  We are calculating Billing cycle already with below DAX, but we believe it is not efficient. Also in 2nd scenario, it is returning the wrong Cycle.

 

Business logic: A billing cycle will complete witht he status of SHIPPED (or anything else than Declined). If my attempts are declined, then the billing cycle would be same till my next attempt for this cycle is <> Declined.  Can we ranked with Order ID as well?

 

DAX : Any simpler calculation would be appreciated.

Billing cycle =
IF ( 'data'[order_status] = "DECLINED", 0, -1 )
    + CALCULATE (
        COUNT ( 'data'[Unique key] ),
        FILTER (
            FILTER (
                ALL ( 'data' ),
                'data'[Unique key] = EARLIER ( 'data'[Unique key] )
                    && RELATED ( 'product'[is_recurring] ) = "YES"
            ),
            [order_status] <> "DECLINED"
                && 'data'[order_id] <= EARLIEST ( 'data'[order_id] )
        )
    )

 

 

 

Scenario 1 (Existing DAX works fine and according to logic).

bill_emailUnique keyAttempt #Billing CycleStatusOIDDate of Sale
0227@gmail.com0227massey@gmail.comPROD1 10SHIPPED107802439/7/2018
0227@gmail.com0227massey@gmail.comPROD1 11SHIPPED1109490310/7/2018
0227@gmail.com0227massey@gmail.comPROD1 12SHIPPED1146914911/6/2018
0227@gmail.com0227massey@gmail.comPROD1 13DECLINED1188785512/6/2018
0227@gmail.com0227massey@gmail.comPROD1 23SHIPPED1190329612/7/2018
0227@gmail.com0227massey@gmail.comPROD1 14DECLINED122660621/6/2019
0227@gmail.com0227massey@gmail.comPROD1 24SHIPPED122792571/7/2019
0227@gmail.com0227massey@gmail.comPROD1 15DECLINED126276182/6/2019
0227@gmail.com0227massey@gmail.comPROD1 25DECLINED126356192/6/2019
0227@gmail.com0227massey@gmail.comPROD1 35SHIPPED126402932/7/2019
0227@gmail.com0227massey@gmail.comPROD1 16SHIPPED130397343/9/2019
0227@gmail.com0227massey@gmail.comPROD1 17SHIPPED133401044/9/2019

 

Scenario 2: The same dax above is giving a wrong result in this table. Look at line # 8 in below table, expected billing cycle value was 7, where as it give 8 as a result of above dax.

bill_emailUnique keyAttempt #Billing CycleExpected ValueStatusOIDDate of SaleLine #
23a@gmail.com23a@gmail.comPROD1100NEW108104209/11/20181
23a@gmail.com23a@gmail.comPROD1111SHIPPED1115680410/11/20182
23a@gmail.com23a@gmail.comPROD1122SHIPPED1154175211/10/20183
23a@gmail.com23a@gmail.comPROD1133SHIPPED1193984012/10/20184
23a@gmail.com23a@gmail.comPROD1144SHIPPED123003081/9/20195
23a@gmail.com23a@gmail.comPROD1155SHIPPED127399762/8/20196
23a@gmail.com23a@gmail.comPROD1166SHIPPED130531223/10/20197
23a@gmail.com23a@gmail.comPROD1187DECLINED133432654/10/20198
23a@gmail.com23a@gmail.comPROD1177SHIPPED133461204/10/20199
23a@gmail.com23a@gmail.comPROD1288DECLINED133497604/11/201910
23a@gmail.com23a@gmail.comPROD1388SHIPPED133645214/13/201911
1 ACCEPTED SOLUTION
v-xicai
Community Support
Community Support

Hi @Anonymous ,

 

You can create column Rank_order to rank the [order_id].

 

Rank_order = RANKX(ALL(data),data[order_id],,ASC)

 

The result using DAX below which have deleted the filter "&& RELATED ('product'[is recurring]) = "YES"" is your expected value, please check if this filter condition is necessary.

 

Billing cycle =

IF ( 'data'[order_status] = "DECLINED", 0, -1 )

+ CALCULATE (

  COUNT ( 'data'[Unique key] ),

  FILTER (

    FILTER (

    ALL ( 'data' ),

   'data'[Unique key] = EARLIER ( 'data'[Unique key] )

    ),

  [order_status] <> "DECLINED"

  && 'data'[Rank_order] <= EARLIEST ( 'data'[Rank_order] )

  ))

 

1.jpg 

 

 

 

 

Here is my test pbix: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EYevNAHt82tMq20JNqL4unsBLypQtj0LEaZoiAnW7eQjOA?e=pFkEVa

 

Best Regards,

Amy

 

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
v-xicai
Community Support
Community Support

Hi @Anonymous ,

 

You can create column Rank_order to rank the [order_id].

 

Rank_order = RANKX(ALL(data),data[order_id],,ASC)

 

The result using DAX below which have deleted the filter "&& RELATED ('product'[is recurring]) = "YES"" is your expected value, please check if this filter condition is necessary.

 

Billing cycle =

IF ( 'data'[order_status] = "DECLINED", 0, -1 )

+ CALCULATE (

  COUNT ( 'data'[Unique key] ),

  FILTER (

    FILTER (

    ALL ( 'data' ),

   'data'[Unique key] = EARLIER ( 'data'[Unique key] )

    ),

  [order_status] <> "DECLINED"

  && 'data'[Rank_order] <= EARLIEST ( 'data'[Rank_order] )

  ))

 

1.jpg 

 

 

 

 

Here is my test pbix: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EYevNAHt82tMq20JNqL4unsBLypQtj0LEaZoiAnW7eQjOA?e=pFkEVa

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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