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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Syndicate_Admin
Administrator
Administrator

Customers turnover - From YTD to Monthly figures in Power Query

Hi everyone,

as you may see from the file I attached I have a dataset of YTD turnovers for each Customer Code and for each month.

My desired output is the one in Column F, that is obtaining Monthly turnover for each Customer Code/Month.

How can I achieve that in Power Query?

 

Thank you so much in advance for any help

 

Mark

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @Syndicate_Admin ,

According to your description, here's my solution.

Add a custom column.

if[DATE]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME])[DATE])then [YTD TURNOVER] else[YTD TURNOVER]-Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME] and x[DATE]=List.Max(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME]and x[DATE]<[DATE])[DATE]))[YTD TURNOVER]{0}

Get the expected result:

vyanjiangmsft_0-1684999299458.png

Here's the whole code, you can copy-paste in a blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldKxCoMwEIDhV5HMB95dEpPMhU4tlK7iINWh0CrE9v2bSAt1yIGDkEM+Ev6kbRXVmmpG1goUIlL6jDNpOLyX1/wcY0VpOM3TME9pURFoxEp1UKDun3IaLn28L1maRnBh4/Kfa1pl5kKZWdywfOxzP8T7kGEDVjippQ21aTg+5jhOt3VXo7+Ua/b7+gSUaLmPD4Ir9yEwwoZiIQ+hEahcyP2o3vmCGLyTaLlQentekOVGDGTKUGxECEyClSOF9U67Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t, #"CUSTOMER CODE" = _t, #"CUSTOMER NAME" = _t, #"CUSTOMER CITY" = _t, #"YTD TURNOVER" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"CUSTOMER CODE", Int64.Type}, {"CUSTOMER NAME", type text}, {"CUSTOMER CITY", type text}, {"YTD TURNOVER", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if[DATE]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME])[DATE])then [YTD TURNOVER] else[YTD TURNOVER]-Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME] and x[DATE]=List.Max(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME]and x[DATE]<[DATE])[DATE]))[YTD TURNOVER]{0})
in
    #"Added Custom"

I attach my sample below for your reference.

 

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

Best regards,

Community Support Team_yanjiang

View solution in original post

4 REPLIES 4
v-yanjiang-msft
Community Support
Community Support

Hi @Syndicate_Admin ,

According to your description, here's my solution.

Add a custom column.

if[DATE]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME])[DATE])then [YTD TURNOVER] else[YTD TURNOVER]-Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME] and x[DATE]=List.Max(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME]and x[DATE]<[DATE])[DATE]))[YTD TURNOVER]{0}

Get the expected result:

vyanjiangmsft_0-1684999299458.png

Here's the whole code, you can copy-paste in a blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldKxCoMwEIDhV5HMB95dEpPMhU4tlK7iINWh0CrE9v2bSAt1yIGDkEM+Ev6kbRXVmmpG1goUIlL6jDNpOLyX1/wcY0VpOM3TME9pURFoxEp1UKDun3IaLn28L1maRnBh4/Kfa1pl5kKZWdywfOxzP8T7kGEDVjippQ21aTg+5jhOt3VXo7+Ua/b7+gSUaLmPD4Ir9yEwwoZiIQ+hEahcyP2o3vmCGLyTaLlQentekOVGDGTKUGxECEyClSOF9U67Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t, #"CUSTOMER CODE" = _t, #"CUSTOMER NAME" = _t, #"CUSTOMER CITY" = _t, #"YTD TURNOVER" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"CUSTOMER CODE", Int64.Type}, {"CUSTOMER NAME", type text}, {"CUSTOMER CITY", type text}, {"YTD TURNOVER", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if[DATE]=List.Min(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME])[DATE])then [YTD TURNOVER] else[YTD TURNOVER]-Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME] and x[DATE]=List.Max(Table.SelectRows(#"Changed Type",(x)=>x[CUSTOMER NAME]=[CUSTOMER NAME]and x[DATE]<[DATE])[DATE]))[YTD TURNOVER]{0})
in
    #"Added Custom"

I attach my sample below for your reference.

 

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

Best regards,

Community Support Team_yanjiang

Syndicate_Admin
Administrator
Administrator

I am trying to create an implicit measure in PowerPivot but there is not an OFFSET function available, so I cannot compile the code.

How can we solve that?

I am working on an Office 365 Subscription.

 

Thank you very much

 

Monthly.pbix

 

ThxAlot_0-1685002468119.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



ThxAlot
Super User
Super User

Monthly.pbix

 

ThxAlot_0-1684940162689.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.

Top Solution Authors
Top Kudoed Authors