Counting back until a change occurs

Craigc3814

Board Regular
Joined
Mar 7, 2016
Messages
217
I am using Excel 2016 Power Query & Power Pivot. I am going to do my best to explain this but I do not know if I can.

I get a massive bill from a vendor each month that has about 350 different account numbers on it. The bill tells me our usage and our charge. It comes in a .txt file

I use PowerQuery to clean that up and load it to a Power Pivot table.

What I need to do is account number by account number look and see how many months it has been since a change to the usage row has changed. So if I have received 6 bills in a row for the same usage, I want a column that returns 6.

I can think of two ways to MAYBE pull it off but you guys are genius here so I figured I would check prior to attempting something awful.

My thought is

A I have a table dedicated to just the most recent bill. It brings in all of the data for the current month bill. I also have the table that compiles all bills as I mentioned earlier. I was thinking there was some way through PowerPivot I tell the current month table to look at the compilation table and return me the first month that DOES NOT match then I could formulate current month minus the bill month returned

B Through PQ Organize Everything in the Compilation table by Account Number then ZtoA on Bill Month. Create some type of index column, then modulo column and subtract the difference to return the 6 months.


Any thoughts would be a game changer for me, I know people like to see the data unfortunately I am not allowed to share it so I will Mimic 1 tables below

Compilation Table
Acct # Month Rate Code Usage Charge
001 4/1/18 129 800 $12,507
001 4/1/18 145 150 $129
001 5/1/18 129 1000 $15,329
001 6/1/18 129 1000 $15,329
001 7/1/18 129 1000 $15,329
002 4/1/18 129 1300 $18,793

So I would want a 3 returned for acct # 001 Rate 129


The Current Month table is the EXACT same format. Only difference is it does not compile bills it just shows this months information for each account
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello Craig,

I am not sure if I have understood correctly but here is how you could do it.
I would use just the compiled bills table ( including current month bills ) and a Date dimension table.

You can see below screenshots:

DEr5kxH.jpg



Then, you can build the below simple model:

dzySlW9.jpg



Then I have built the below Pivot Table, where, by using the pivot table filter, I can see ( from the point of view of the date in the filter ) which was the previous date with a different rate:

mfHEupw.jpg



The measure are used are simple:

CurrentRate =<br><span class="Keyword" style="color:#0070FF">IF</span><span class="Parenthesis" style="color:#969696"> (</span> <span class="Keyword" style="color:#0070FF">HASONEVALUE</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Acct #] <span class="Parenthesis" style="color:#969696">)</span>, <span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Rate] <span class="Parenthesis" style="color:#969696">)</span> <span class="Parenthesis" style="color:#969696">)</span><br>

Number of Months =<br><span class="Keyword" style="color:#0070FF">VAR</span> <span class="Variable" style="color:#49b0af">PrevDte</span> = [Previous Date With Different Rate]<br><span class="Keyword" style="color:#0070FF">RETURN</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">IF</span><span class="Parenthesis" style="color:#969696"> (</span><br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">ISBLANK</span><span class="Parenthesis" style="color:#969696"> (</span> <span class="Variable" style="color:#49b0af">PrevDte</span> <span class="Parenthesis" style="color:#969696">)</span>,<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">BLANK</span><span class="Parenthesis" style="color:#969696"> (</span><span class="Parenthesis" style="color:#969696">)</span>,<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">DATEDIFF</span><span class="Parenthesis" style="color:#969696"> (</span> <span class="Variable" style="color:#49b0af">PrevDte</span>, <span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Periods[Date] <span class="Parenthesis" style="color:#969696">)</span>, <span class="Keyword" style="color:#0070FF">MONTH</span> <span class="Parenthesis" style="color:#969696">)</span><br><span class="indent4">    </span><span class="Parenthesis" style="color:#969696">)</span><br>

Previous Date With Different Rate =<br><span class="Keyword" style="color:#0070FF">IF</span><span class="Parenthesis" style="color:#969696"> (</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">HASONEVALUE</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Acct #] <span class="Parenthesis" style="color:#969696">)</span>,<br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">VAR</span> <span class="Variable" style="color:#49b0af">CurRate</span> =<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Rate] <span class="Parenthesis" style="color:#969696">)</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">VAR</span> <span class="Variable" style="color:#49b0af">CurPeriod</span> =<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Periods[Date] <span class="Parenthesis" style="color:#969696">)</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">VAR</span> <span class="Variable" style="color:#49b0af">Res</span> =<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">CALCULATE</span><span class="Parenthesis" style="color:#969696"> (</span><br><span class="indent8">        </span><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Month] <span class="Parenthesis" style="color:#969696">)</span>,<br>            Charges[Rate] <> <span class="Variable" style="color:#49b0af">CurRate</span>,<br>            Periods[Date] < <span class="Variable" style="color:#49b0af">CurPeriod</span><br><span class="indent8">        </span><span class="Parenthesis" style="color:#969696">)</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">RETURN</span><br><span class="indent8">        </span><span class="Variable" style="color:#49b0af">Res</span>,<br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">BLANK</span><span class="Parenthesis" style="color:#969696"> (</span><span class="Parenthesis" style="color:#969696">)</span><br><span class="Parenthesis" style="color:#969696">)</span><br>

Previous Rate =<br><span class="Keyword" style="color:#0070FF">VAR</span> <span class="Variable" style="color:#49b0af">PrevDte</span> = [Previous Date With Different Rate]<br><span class="Keyword" style="color:#0070FF">RETURN</span><br><span class="indent4">    </span><span class="Keyword" style="color:#0070FF">IF</span><span class="Parenthesis" style="color:#969696"> (</span><br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">ISBLANK</span><span class="Parenthesis" style="color:#969696"> (</span> <span class="Variable" style="color:#49b0af">PrevDte</span> <span class="Parenthesis" style="color:#969696">)</span>,<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">BLANK</span><span class="Parenthesis" style="color:#969696"> (</span><span class="Parenthesis" style="color:#969696">)</span>,<br><span class="indent8">        </span><span class="Keyword" style="color:#0070FF">CALCULATE</span><span class="Parenthesis" style="color:#969696"> (</span> <span class="Keyword" style="color:#0070FF">MAX</span><span class="Parenthesis" style="color:#969696"> (</span> Charges[Rate] <span class="Parenthesis" style="color:#969696">)</span>, Periods[Date] = <span class="Variable" style="color:#49b0af">PrevDte</span> <span class="Parenthesis" style="color:#969696">)</span><br><span class="indent4">    </span><span class="Parenthesis" style="color:#969696">)</span><br>


I hope this will help you find your solution
 
Upvote 0
This is very close to what I did. Instead I created a table of the 12 most recent invoices with a column showing invoice month. I then made a calculated column called months ago where I took the current month and year then subtracted from the invoice month and year. This gives me how many months ago the bill occurred.

From there I went into powerBI and did a lookup on the max value and returned the months ago column.

My issue is now I need to do a lookup in powerBI for the second highest, and for the 3rd highest.

Thanks for always answering my posts!!! Much appreciated.
 
Last edited:
Upvote 0
My issue is now I need to do a lookup in powerBI for the second highest, and for the 3rd highest.

Hi Craig, what do you mean by 2nd highest and 3rd highest ? Can you show me an example of these would be?
 
Upvote 0
It's government data in a sense so I can't exactly post it. Basically Max brings back the highest value. From there I look up the months ago field off of the highest value and I know how long ago it was. I then need to say Okay, what's the second highest usage month and how many months ago was that.

I'm doing all of this to do a really weird calculation at the end. We get billed on the highest usage month 50% of the usage for the next 12 months. Once that 12 months falls off it needs to return the next highest value and calculate that out for 12 months at 50%.

Overly confusing I understand. I'm really close, if I can get the second highest value by asset to return the. Then third highest (I think rank is what I need to do).
 
Upvote 0
Powerquery / M
Start with single table called Table1 that has all the data in it [Acct#,Month,Rate,Code,Usage] including most recent month
Code below will generate table picking out the most recent date, the related Usage, and the number of consecutive months with that usage for same Acct#/Rate

qBase
Code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Base = Table.TransformColumnTypes(Source,{{"Acc#", type text}, {"Month", type date}, {"Rate", type text}, {"Code", type text}, {"Usage", Int64.Type}})
in Base

fnTry
Code:
(Z1 as text, Z2 as text, Z3 as date) =>
let source=  qBase,
#"Filtered Rows" = Table.SelectRows(source, each [#"Acc#"] = Z1 and [Rate] = Z2 and [Month] = Z3),
Z4 = Date.AddMonths(Z3,-1),
#"Filtered Rows2" = Table.SelectRows(source, each [#"Acc#"] = Z1 and [Rate] = Z2 and [Month] = Z4),
Z5= #"Filtered Rows"{0}[Usage],
Z6= if Table.RowCount(#"Filtered Rows2") = 0 then null else #"Filtered Rows2"{0}[Usage],
Compare = if Z5<>Z6 or Z6=null then 0 else fnTry(Z1,Z2,Z4) + 1
in Compare

qMax
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Base = Table.TransformColumnTypes(Source,{{"Acc#", type text}, {"Month", type date}, {"Rate", type text}, {"Code", type text}, {"Usage", Int64.Type}}),
    #"Grouped Rows" = Table.Group(Base, {"Acc#", "Rate"}, {{"Max", each List.Max([Month]), type date}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows",{"Acc#", "Rate", "Max"},Base,{"Acc#", "Rate", "Month"},"Base",JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Base", {"Usage"}, {"Usage"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table1", "Custom", each fnTry([#"Acc#"], [Rate], [Max])),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"Custom", 0}}),
    #"Calculated Absolute Value" = Table.TransformColumns(#"Replaced Errors",{{"Custom", each _+1, type number}})
in
    #"Calculated Absolute Value"
 
Upvote 0
PowerBi's Max or Rank function seem like much better options. It will limit the amount of code the query is running and speed the tool up. It will have 3 years worth of invoices in it with about 500 lines per invoice.
 
Upvote 0
It's government data in a sense so I can't exactly post it. Basically Max brings back the highest value. From there I look up the months ago field off of the highest value and I know how long ago it was. I then need to say Okay, what's the second highest usage month and how many months ago was that.

I'm doing all of this to do a really weird calculation at the end. We get billed on the highest usage month 50% of the usage for the next 12 months. Once that 12 months falls off it needs to return the next highest value and calculate that out for 12 months at 50%.

Overly confusing I understand. I'm really close, if I can get the second highest value by asset to return the. Then third highest (I think rank is what I need to do).

So, following my first post, I am assuming I am showing the previous value and how long ago it was. Then you also want to know the previous value prior to the previous value and how long ago this was ?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top