cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
manojsv16 Regular Visitor
Regular Visitor

Measure to calculate count of accounts which has 2 or more consecutive bill months

Hello All,

 

I have a requriement to create a measure to calculate the count the accounts which has 2 or more consecutive bill months.

Please help me in getting the total of count of account which has the consective month bills 

 

I would appreciate any help on achieving this.  Also, please let me know if you need any further information from my end. 

 

Example: 

The total count i expected to get from the table below is 6. 

 

Account 11234  has consective bills in Dec-18 to Jan-19,  Mar-19 to Apr 19.  This account doesn't have bill in the month of Feb-19

So the count for Account 11234 is 2 (1 for Dec-18 & Jan-19, 1 for Mar-19 & Apr 19). 

 

Account 87239 has consective bills in Dec-18 to  Mar-19 , May-19 to Jun-19. This account doesn't have bill in the month of Apr-19

So the count for Account 87239 is 2 (1 for Dec-18 to  Mar-19, 1 for  May-19 to Jun-19). 

 

Account 54327 has consective bills in Jan-19 to  Feb-19 , Apr-19 to Jun-19. This account doesn't have bill in the month of Mar-19

So the count for Account 54327 is 2 (1 for Jan-19 to  Feb-19 , 1 for  Apr-19 to Jun-19)

 

 

AccountBill Month
11234Dec-18
11234Jan-19
11234Mar-19
11234Apr-19
87239Dec-18
87239Jan-19
87239Feb-19
87239Mar-19
87239May-19
87239Jun-19
54327Jan-19
54327Feb-19
54327Apr-19
54327May-19
54327Jun-19

 

Thank you!!

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hey @manojsv16 ,

 

basically the restart of the index is not adding an increment to an exisiting variable under certain circumstances as one might use in different programming languages, this is due to the lack of "real" iterators like for/next or do/while (but we also should not underestimate the power of table iterators).

Instead the reindexing is based on "simple" counting. What's been counted is the number of rows, that is closest (the one the closest to the "current" Month" the "RunningMonthIndex") to the Month that is flagged by the "_check NoValue" Measure. I guess in a way it's valid to say that the "last" blank value interrupts the counting and restarts the index.

 

Hopefully this provides the necessary information you are looking for.

Regards,
Tom

 

P.S.: Maybe you might consider to select one of the posts in this thread as an answer, as this might also help others.

 

Hamburg - Germany
If I answer your question, please mark my post as solution, this will also help others.
Proud to be a Datanaut!

View solution in original post

20 REPLIES 20
Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

@manojsv16 

 

Would you like to do this in M /Power Query or you need a DAX measure only?

 

 

manojsv16 Regular Visitor
Regular Visitor

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hi Zubair,

 

Thanks a lot for the response!!

 

I prefer DAX Measures. But, i am fine with M/ Power Query if have a better approach to solve this issue.

 

 

Thanks!!

 

Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

HI @manojsv16 

 

Here is a Power Query Solution

Please see attached file for steps

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ0MjZR0lFySU3WNbRQitVBCHkl5ukaWqII+SYWoQs5FsCFLMyNjC1RzYIJIZkFE3JLTUIXQjIeIVSJLuRVCjfL1MTYyBzVeJgQkvEwISSnwoSQjIebBTU+FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Account = _t, #"Bill Month" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Bill Month", type date}}),
     AllMonths={List.Min(List.Transform(BeginningMonth[Bill Month],each Date.Year(_)))..
            List.Max(List.Transform(BeginningMonth[Bill Month],each Date.Year(_)))},
     MyTable= Table.AddColumn(Table.FromColumns({AllMonths},{"Year"}),"Months",each {1..12}),
    ExpandedMonths = Table.ExpandListColumn(MyTable, "Months"),
    #"Added Custom" = Table.AddColumn(ExpandedMonths, "Custom", each #date([Year],[Months],1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Year", "Months"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns1", "Account", each List.Distinct(Source[Account])),
    #"Expanded Account" = Table.ExpandListColumn(#"Added Custom1", "Account") ,     
    BeginningMonth = Table.AddColumn(#"Changed Type", "Custom", each Date.StartOfMonth([Bill Month])),
    #"Removed Columns" = Table.RemoveColumns(BeginningMonth,{"Bill Month"}),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Account",{"Account", "Custom"},#"Removed Columns",{"Account", "Custom"},"Removed Columns",JoinKind.LeftOuter),
    #"Expanded Removed Columns" = Table.ExpandTableColumn(#"Merged Queries", "Removed Columns", {"Account"}, {"Account.1"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Removed Columns",{{"Account", Order.Ascending}, {"Custom", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Account.1"}, {{"Consecutive Dates", each List.Transform(_[Custom], each Date.ToText(_,"MM-yyyy"))}, {"ConsecutiveCount", each Table.RowCount(_), type number}},GroupKind.Local),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Account.1] <> null)),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [ConsecutiveCount] >= 2),
    #"Extracted Values" = Table.TransformColumns(#"Filtered Rows1", {"Consecutive Dates", each Text.Combine(List.Transform(_, Text.From), ","), type text})
in
    #"Extracted Values"

7Mar19.png

manojsv16 Regular Visitor
Regular Visitor

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hi Zubair,

 

Thanks a lot for the response!!

 

I tried the metioned solution and it works fine. However, there are some limitation in implementing the solution since direct query was used to get the data and limited options to do transformation. 

 

I would appreciate if you could help me in getting a measure to solve the issue.

 

Thanks!!

Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hi@manojsv16

Tagging some DAX experts to help you.

 

@MFelix @TomMartens @Greg_Deckler 


Highlighted
Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

I'm sure that there are better ways but I did this in 4 calculated columns:

 

Column = 
IF(MONTH([Bill Month]) = 1,12,MONTH([Bill Month]) - 1)

Column 2 = 
COUNTROWS(FILTER(ALL('Table11'),[Account] = EARLIER([Account]) && MONTH([Bill Month]) = EARLIER([Column])))

Column 3 = 
IF(MONTH([Bill Month]) = 12,1,MONTH([Bill Month]) + 1)

Column 4 = 
VAR __table = FILTER('Table11',[Account] = EARLIER([Account]) && MONTH([Bill Month]) = EARLIER([Column 3]) && [Column 2] = 1)
RETURN
IF(ISBLANK(COUNTROWS(__table)),1,BLANK())

The SUM of Column 4 equals 6. Table 11 of attached.


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

manojsv16 Regular Visitor
Regular Visitor

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hi Greg,

 

Thanks a lot for the response!!

 

I tried the solution provied, it is very useful and working fine. I am using direct query and trying to avoid calculated columns as well. 

I can create the column and column 3 in database. Direct Query doesn't allow to include countrows DAX expression. 

 

However, I would like to check whether there is way to calcuate the Column 3 and Column 4 using DAX Measure instead of calculated columns  

 

I would appreciate for any suggestion on acheiving the result. 

 

Thank you!!

Super User
Super User

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hey,

 

this pbix file contains a measure based solution 🙂

It's not based on your sample data, but I think it can be easily adjusted to your data model.

There are some requirements or let's say assumptions regarding the underlying data model:

  • There is a dedicated calendar table
  • The calendar table contains a column that counts the number of months, meaning a calendar table that starts on 2017-01-01 will count the January 2018 as the 13th month in your calendar table.

This is the data of my fact table:

CustomerDatequantity

A 2018-01-01 2
A 2018-03-01 3
A 2018-04-01 6
A 2018-09-01 2
A 2018-10-01 3
A 2018-11-01 7
A 2018-12-01 3
B 2017-12-01 4
B 2018-01-01 6
B 2018-03-01 5
B 2018-04-01 3


Both customers have "orders" in March and April 2018. This means the count of customers in April 2018 should return 2:

image.png

 

 This solution is based on this DAX statement that creates a table that contains some general information about consecutive events in a table, I use this snippet in measures that derive more, meaning other information from these events:

 

...
FILTER(
            ADDCOLUMNS(
                ADDCOLUMNS(
                    SUMMARIZE(
                        'fact'
                        ,'Customer'[Customer]
                        ,'Calendar'[RunningMonthIndex]
                    )
                    ,"quantity", [Total quantity]
                    ,"prevMonthIndex", [_prevMonthIndex]
                )
                ,"Distance prevMonth", [RunningMonthIndex] - [prevMonthIndex] 
            )
            ,[Distance prevMonth] = DistanceOfMonth
        )
...

The snippet above is used in the measure 

 

NumberOfCustomer = 
var DistanceOfMonth = 1
return
COUNTROWS(
    SUMMARIZE(
        ...
        ,[Customer]
    )
)

 

I wrap a COUNTROWS(SUMMARIZE(...)) around the table snippet. The SUMMARIZE(...,[Customer]) returns a table with a single column and COUNTROWS(...) just counts the number of rows. Please don't get confused by the notation of 

...
,[Customer]
...

Here I do not reference a measure (no table name is specified), but a column that is contained inside the table virtual table used by the DAX function, namely SUMMARIZE(...). I'm not aware if this can be considered a best practice, but at least it helps me to compose my DAX statements :-), and as it does not interfere with the lineage or performance, I don't care.

 

Regards,

Tom

Hamburg - Germany
If I answer your question, please mark my post as solution, this will also help others.
Proud to be a Datanaut!
manojsv16 Regular Visitor
Regular Visitor

Re: Measure to calculate count of accounts which has 2 or more consecutive bill months

Hi Tom,

 

Thanks for the response!! I appreciate your effort on trying to solve the issue.

 

I tried the solution provided and it is working fine.  The solution covered most of the requirement, but there is small observations mentioned below.  

 

  • I wanted the count of bills for 2 or more consecutive months for a account.  The total count expected from the data model provided by you  is 4.  But, the total count i am getting from the measure (NumberofCustomer) is 2 which is the count of customers. 
    •  Customer A has 2 or more consecutive quantity in the month (2018-03-01 - 2018-04-01 & 2018-09-01 - 2018-12-01)
    •  Customer B has 2 or more consecutive quantity in the month (2017-12-01 - 2018-01-01 & 2018-03-01 - 2018-04-01)

 Please help me in acheving the below result. I would appreciate any suggestions. 

 

Thank you in advance!!

ConsectiveBilling.PNG

 

Regards,

Manoj

 

Helpful resources

Announcements
Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Ask Amanda Anything Q&A

Ask Amanda Anything Q&A

Learn the answers to some of the questions asked during the Amanda Triple A event.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Users Online
Currently online: 135 members 1,721 guests
Please welcome our newest community members: