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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Water
Helper II
Helper II

DAX to count the number of times, for each customer, an arrears amount moves 3 consecutive months

Power Bi Experts, please help with the correct DAX for the below calculation?

 

Summary: I want to flag non-paying customers becoming a risk - by counting the times an unpaid arrears amount for each customer “moves” over three consecutive months.

 

Monthly I receive a dataset with customers with their amounts in arrears. The arrear amounts are grouped in one of four monthly progressive groups: 30-59 days, 60-89 days, 90-119 days, and over 120 days.

 

Let say John owes $30 as part of the "30-59 days" group in April 2023 but then doesn’t pay it, and that $30 moves to the "60-89 days" arrears in May 2023, and then to the "90-119 days" group in June 2023, it will probably end up in the "over 120 days" in July 2023.

If this same patterns continues for all his arrear amounts as times passes, moving from "30-59 days" to "90-119 days" over three consecutive months, John is to be flagged as a risk to be monitored more closely.

 

So my idea is to count the number of times, for each customer, a specific arrears amount moves from the:

  • "30-59 days" category to the "90-119 days" category (with each such movement counting as 1).

Question: Please help me with the DAX to accomplish the above?

 

The aim is to create a table ranking the customers with counts per year sorting from highest count to lowest. Ideal output will look something like this:

Water_0-1730418188593.png

 

 

 

 

The PBIX file is here with the example date as copied below. (Has a date table.) 

 

Water_0-1730415244818.png

 

To calculate for Peter, the logic is as follows:

 

Water_0-1730418448073.png

 

 

 

Thank you very much!

 

W

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @Water ,

 

I'm afraid your requirements might not be achievable through DAX alone. I would recommend first formatting the data in Power Query to better align with your needs. I've made a test for your reference:

vbofengmsft_0-1730442500773.png

1\ I assume there is a excel table

vbofengmsft_1-1730442638770.png

2\Edit Query

a)SortByCustomer Name and Date

vbofengmsft_2-1730442756812.png

 

b)add an index column

vbofengmsft_3-1730442774947.png

c)Add a count column to count rows that meet your neeeds

    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),

d)Change type

    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),

The whole power query code

let
    Source = Excel.Workbook(File.Contents("C:\Users\Bof\Desktop\Book111.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Account Number", Int64.Type}, {"Customer Name", type text}, {"Date", type date}, {"$ Amount of30 - 59 Days in Arrears", Int64.Type}, {"$ Amount of 60 - 89 Days in Arrears", Int64.Type}, {"$ Amount of  90 - 119 Days in Arrears", Int64.Type}, {"$ Amount of Over 120 Days in Arrears", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer Name", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"CountNumber", Int64.Type}})
in
    #"Changed Type1"

vbofengmsft_4-1730442898280.png

3\Edit in report

vbofengmsft_5-1730442923919.png

 

Best Regards,

Bof

 

 

 

View solution in original post

SachinNandanwar
Super User
Super User

You can do it through DAX
First create a CalculatedTable :

SachinNandanwar_0-1730537603432.png

and then a measure in that table

SachinNandanwar_1-1730537636060.png

and then create a final output table

SachinNandanwar_2-1730537667786.png


Output :

SachinNandanwar_3-1730537694808.png

File attached here : https://easyupload.io/t42nqk



Regards,
Sachin
Check out my Blog

View solution in original post

6 REPLIES 6
SachinNandanwar
Super User
Super User

You can do it through DAX
First create a CalculatedTable :

SachinNandanwar_0-1730537603432.png

and then a measure in that table

SachinNandanwar_1-1730537636060.png

and then create a final output table

SachinNandanwar_2-1730537667786.png


Output :

SachinNandanwar_3-1730537694808.png

File attached here : https://easyupload.io/t42nqk



Regards,
Sachin
Check out my Blog

Thank you so much, @SachinNandanwar ! 

 

You solution works fantastically. What I really appreciate is how well this works on a massive dataset. 

 

Sorry for taking so long to get back to you. There was some problem which took me a while to figure out. 

 

(May I be so forward to ask you to please have a look at my other question and see if you can suggest something just as brilliant?)

 

Thanks and best regards, 

 

W

No worries..Glad to help and thank you for the feedback.



Regards,
Sachin
Check out my Blog
Anonymous
Not applicable

Hi @Water ,

 

I'm afraid your requirements might not be achievable through DAX alone. I would recommend first formatting the data in Power Query to better align with your needs. I've made a test for your reference:

vbofengmsft_0-1730442500773.png

1\ I assume there is a excel table

vbofengmsft_1-1730442638770.png

2\Edit Query

a)SortByCustomer Name and Date

vbofengmsft_2-1730442756812.png

 

b)add an index column

vbofengmsft_3-1730442774947.png

c)Add a count column to count rows that meet your neeeds

    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),

d)Change type

    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),

The whole power query code

let
    Source = Excel.Workbook(File.Contents("C:\Users\Bof\Desktop\Book111.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Account Number", Int64.Type}, {"Customer Name", type text}, {"Date", type date}, {"$ Amount of30 - 59 Days in Arrears", Int64.Type}, {"$ Amount of 60 - 89 Days in Arrears", Int64.Type}, {"$ Amount of  90 - 119 Days in Arrears", Int64.Type}, {"$ Amount of Over 120 Days in Arrears", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer Name", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "CountNumber", each if [Index] < 3 or [#"$ Amount of  90 - 119 Days in Arrears"]= null  then 0 else if [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of 60 - 89 Days in Arrears"){[Index]-1} and [#"$ Amount of  90 - 119 Days in Arrears"]=Table.Column(#"Added Index", "$ Amount of30 - 59 Days in Arrears"){[Index]-2} then 1 else 0),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"CountNumber", Int64.Type}})
in
    #"Changed Type1"

vbofengmsft_4-1730442898280.png

3\Edit in report

vbofengmsft_5-1730442923919.png

 

Best Regards,

Bof

 

 

 

Thank you again for you help.

Unfortunately your solution is not working, but this is because my example data was not sufficient in replicating the real-life dataset. I am sorry, this is my mistake. Your solution will only work where there is one row per customer per month as was per my initial example data. As I was looking more closely at my actual data, I saw each customer might have more than one entry per month as per various Rate Codes or Contract numbers, so more than one amount in each of the arrears categories. Below you can see e.g. Japie with multiple rows just in July 2024. (Or here is Japie's complete record in Excel with two other customers.)

 

 

Water_0-1731105721482.png

 

 

This means the indexing part of the solution does not work.

 

Also, with more than 6 million rows, it takes very long to execute your solution. The other solution provided by SachinNandanwar afterwards seems to work better, and I am busy testing it.

 

Thanks again for you effort.

 

Best regards, 

 

W

Thank you very much! Really appreciate your help and time spend on this. I am excited to study your solution and test it on my real dataset. Will definately accept as solution if all works well. Please give me a couple of days. Best wishes.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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