• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Cascading Queries|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Cascading Queries|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryCascading Queries
sp_PrintTopic sp_TopicIcon
Cascading Queries
Avatar
Franz Lurvink

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
May 27, 2020
sp_UserOfflineSmall Offline
1
May 27, 2020 - 6:24 pm
sp_Permalink sp_Print

Hi

I am trying to build a cost allocation model. The idea is relatively simple. I have amounts on cost centres and allocate them to other cost centres. The issue is that the allocation is cascading i.e. cost centre 1 needs to be allocated then cost centre 2 (including the share of cost centre 1) needs to be allocated.

Here is where I am struggling as a newbie: There are not that many cost centres and I could do it with queries one by one. But this will still create a bunch of queries and I imagine there must be a smarter way or technique to do it.

Question:  has anyone already done something like this or can recommend a smart approach?

 

Many thanks in advance.

Franz

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
May 28, 2020 - 10:52 pm
sp_Permalink sp_Print

Hi Franz,

I'm a newbie in cascading costs, so it will be important for me to see a sample of the source data and how the result should be.

Please upload a file with source data and a manual example of the desired result.

Thank you

Avatar
Franz Lurvink

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
May 27, 2020
sp_UserOfflineSmall Offline
3
June 2, 2020 - 10:16 pm
sp_Permalink sp_Print

Hi Catalin

I attached a sample for what I am trying to do.

Currently I applied a solution by calculating the cost allocation with formulas in excel directly. But I am sure there must be a better way in power query.

Any idea how to improve would be highly appreciated.

 

Cheers

Franz

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
June 5, 2020 - 10:22 pm
sp_Permalink sp_Print

Hi Franz,

That should not be called "Cascading cost", it's tornading costs... Almost twisted my brains.

I had to build a recursive function to get the results: (this is the "Convert" function, used in the main query)

(OrigTable,Allocations,a,b,lst)=>
let

//get conversion percentage
Key=(k1,k2)=>
let
FilteredRows = try Table.SelectRows(Allocations, each ([From] = k2) and ([To] = k1))[Key]{0} otherwise 0
in
FilteredRows,

//get the original amount
Orig=(CC as number) as number=>
let
Result=Table.SelectRows(OrigTable, each _[CC]=CC)
in
Result[Amount]{0},

Result = if List.IsEmpty(lst) then
0
else
List.Accumulate(lst,0,(state,current)=>state+Convert(OrigTable,Allocations,b,current,List.Select(List.Distinct(Allocations[From]), each _ < current ) ) )
in
Key(a,b) * (Orig(b) + Result)

 

And here is the main query:

let
Source = Excel.CurrentWorkbook(){[Name="BaseData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", Int64.Type}, {"CC", Int64.Type}, {"Amount", type number}}),
RemovedColumns = Table.RemoveColumns(#"Changed Type",{"Period"}),
Allocation=Excel.CurrentWorkbook(){[Name="Allocation"]}[Content],
AllocationChanged = Table.TransformColumnTypes(Allocation,{{"Sequence", Int64.Type}, {"From", Int64.Type}, {"To", Int64.Type}, {"Account", Int64.Type}, {"Account desc.", type text}, {"Key", Percentage.Type}}),
#"Added Custom" = Table.AddColumn(RemovedColumns, "Convert To", each List.Distinct(AllocationChanged[From])),
#"Expanded Columns" = Table.ExpandListColumn(#"Added Custom", "Convert To"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Columns",{{"Convert To", Int64.Type}}),
#"Added Custom2" = Table.AddColumn(#"Changed Type1", "Custom.1", (x)=> Convert(RemovedColumns, AllocationChanged,x[CC],x[Convert To],List.Select(List.Distinct(AllocationChanged[From]), each _ < x[Convert To] ))),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom2",{{"Custom.1", type number}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type2", "Allocation", each if [Custom.1]>0 then [Custom.1] else 0),
#"Added Custom3" = Table.AddColumn(#"Added Custom1", "Release", each if [Custom.1]<0 then [Custom.1] else 0),
#"Changed Type3" = Table.TransformColumnTypes(#"Added Custom3",{{"Allocation", type number}, {"Release", type number}})
in
#"Changed Type3"

Avatar
Franz Lurvink

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
May 27, 2020
sp_UserOfflineSmall Offline
5
June 8, 2020 - 7:11 pm
sp_Permalink sp_Print

Hi Catalin

I don't know what to say. Putting so much work into my problem is very generous.

Thanks a million for the attachment. When I tried to use your code directly an error popped up on the CONVERT function. But the attached workbook works fine.

To be honest:  Your solution is quite a challenge for me. I don't fully understand it yet ... but I will study it thoroughly (which will certainly keep me quite busy).

Again, many many thanks.

 

Best regards

Franz

sp_AnswersTopicAnswer
Answers Post
sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Roy Lutke, Richard West, Jeff Krueger, Tom VAN LOO, Kylara Papenfuss, Nada Perovic
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.