+ Reply to Thread
Results 1 to 9 of 9

Creating new column in power query with multiple criteria

Hybrid View

naea07 Creating new column in power... 04-21-2020, 10:34 AM
naea07 Re: Creating new column in... 04-21-2020, 10:38 AM
AliGW Re: Creating new column in... 04-21-2020, 10:39 AM
naea07 Re: Creating new column in... 04-21-2020, 10:40 AM
AliGW Re: Creating new column in... 04-21-2020, 10:55 AM
naea07 Re: Creating new column in... 04-21-2020, 11:19 AM
AliGW Re: Creating new column in... 04-21-2020, 11:26 AM
Olly Re: Creating new column in... 04-28-2020, 08:37 AM
Bo_Ry Re: Creating new column in... 04-28-2020, 01:34 PM
  1. #1
    Registered User
    Join Date
    04-07-2020
    Location
    thailand
    MS-Off Ver
    -
    Posts
    4

    Creating new column in power query with multiple criteria

    Hi, currently the information that in have in power query is the stock quantity that each branch (Branch A, B, C, D, E) has and the requested qty that is to be transfer from Branch A to other branches.

    What I would like is to have a new column (the yellow highlighted ones) in this power query to show the stock (from branch A) that can be transfer to another branches (B, C, D, E), given that the order of the transfer will always be prioritize to B first then C then D then E.

    (I could write this through normal formula in excel but this process will be repeated by many times for different dataset so i think doing in power query will be much more neat and flexible)

    How can this be done? Thanks for the help in advance

    Attachment 673651
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by naea07; 04-21-2020 at 11:40 AM.

  2. #2
    Registered User
    Join Date
    04-07-2020
    Location
    thailand
    MS-Off Ver
    -
    Posts
    4

    Re: Creating new column in power query with multiple criteria

    Attached the excel file
    Attached Files Attached Files
    Last edited by naea07; 04-21-2020 at 11:42 AM.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Creating new column in power query with multiple criteria

    Welcome to the forum.

    There are instructions at the top of the page explaining how to attach your sample workbook.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  4. #4
    Registered User
    Join Date
    04-07-2020
    Location
    thailand
    MS-Off Ver
    -
    Posts
    4

    Re: Creating new column in power query with multiple criteria

    thank you, i just got it

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Creating new column in power query with multiple criteria

    Sorry - do you mean you have solved the issue?

    If you still need help, please explain the results you are looking for - I don't see the logic.

  6. #6
    Registered User
    Join Date
    04-07-2020
    Location
    thailand
    MS-Off Ver
    -
    Posts
    4

    Re: Creating new column in power query with multiple criteria

    No it's not solved yet. When I reply I got it, i meant I got how to upload the excel file part.

    The logic behind the number is that item X has 33 stock available for transfer. By going through the branch in the order of priority:
    - Branch B has requested no stock so the stock of Branch A remains at 33
    - Branch C has requested 31 stock so 31 will be transfer from A to C, leaving A with 2 stock
    - Branch D has requested 8, but A has only 2 left so the stock to be transfer will be 2.

    The stock that is to be transfer to each branch will be from Branch A only, and the order of the transfer will always be B, C, D then E.

    Hope this makes it more clear. Thanks.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Creating new column in power query with multiple criteria

    Thanks. The incremental nature of what you want to achieve is above my pay grade in PQ - sorry. Hopefully Olly or another PQ wizard will see this.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Creating new column in power query with multiple criteria

    You could use a query like:

    let
        fnSourceStock = (ItemData, Item) =>
            let
                #"Filtered ItemData" = Table.SelectRows(ItemData, each ([Item number] = Item) and ([Branch] = "A")),
                #"Source Stock" = List.Sum(#"Filtered ItemData"[Current stock])
            in
                #"Source Stock",
    
        fnPriorDemand = (ItemData, Item, Branch) =>
            let
                #"Filtered ItemData" = Table.SelectRows(ItemData, each ([Item number] = Item) and ([Branch] <> "A") and ([Branch] < Branch)),
                #"Prior Demand" = List.Sum(#"Filtered ItemData"[To be transfer from Branch A])
            in
                #"Prior Demand",
    
        Source = Excel.CurrentWorkbook(){[Name="ItemData"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item number", type text}, {"Branch", type text}, {"Current stock", Int64.Type}, {"To be transfer from Branch A", Int64.Type}}),
        #"Added Source Stock" = Table.AddColumn(#"Changed Type", "Source Stock", each fnSourceStock(#"Changed Type", [Item number]), Int64.Type),
        #"Added Prior Demand" = Table.AddColumn(#"Added Source Stock", "Prior Demand", each fnPriorDemand(#"Changed Type", [Item number], [Branch]), Int64.Type),
        #"Added Available Qty" = Table.AddColumn(#"Added Prior Demand", "Available To Transfer", each List.Max ({0, [Source Stock] - [Prior Demand]}), Int64.Type),
        #"Added Transfer Qty" = Table.AddColumn(#"Added Available Qty", "Transfer From Source", each List.Min({[Available To Transfer], [To be transfer from Branch A]}), Int64.Type),
        #"Removed Columns" = Table.RemoveColumns(#"Added Transfer Qty",{"Source Stock", "Prior Demand", "Available To Transfer"})
    
    in
        #"Removed Columns"


    However - I'd use PowerPivot instead.

    First of all, I'd create a Branches table - as I'm sure your branch names won't always be in conveniently alphabetical order - so this table establishes the source branch, and the transfer priority of the remainder.

    tables.png

    I'd load both tables to the data model (using Power Query) and create a relationship on [Branch]:

    relationship.png

    Now you can add a calculated column to table ItemData:

    TransferFromSource:=
    VAR SourceStock = 
    	CALCULATE ( 
    		SUM ( ItemData[Current stock] ),
    		ALLEXCEPT ( ItemData, ItemData[Item number] ),
    		Branches[Priority] = "Source"
    	)
    VAR CurrentPriority = 
    	RELATED ( Branches[Priority] )
    VAR PriorDemand = 
    	CALCULATE ( 
    		SUM ( ItemData[To be transfer from Branch A] ),
    		ALLEXCEPT ( ItemData, ItemData[Item number] ),
    		FILTER ( 
    			ALL ( Branches[Priority] ),
    			Branches[Priority] <> "Source" && Branches[Priority] < CurrentPriority
    		)
    	)
    VAR AvailableToTransfer = 
    	MAX ( 0, SourceStock - PriorDemand )
    RETURN
    	MIN ( AvailableToTransfer, ItemData[To be transfer from Branch A] )
    Gives your expected results:
    pp_output.png
    Attached Files Attached Files
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  9. #9
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,222

    Re: Creating new column in power query with multiple criteria

    Another PQ


    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        Stock2A = Table.AddColumn(Source, "Stocks to be transfer from A",(x)=> 
    List.Max({0,List.Min({List.Sum(Table.SelectRows(Source, each [#"Item number"] = x[#"Item number"]  and [Branch] =" A")[Current stock] )  - 
    List.Sum(Table.SelectRows(Source, each [#"Item number"] = x[#"Item number"]  and [Branch] <x[Branch])[To be transfer from Branch A]),  
    List.Sum(Table.SelectRows(Source, each [#"Item number"] = x[#"Item number"]  and [Branch] = x[Branch])[To be transfer from Branch A]) })}))
    in
        Stock2A
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 02-17-2020, 06:03 AM
  2. Custom Permutations Column in Power Query from two columns in the same Query
    By PaintPaddy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2019, 02:48 PM
  3. [SOLVED] Creating Dynamic Parameters in Excel from link to Power Query
    By Stefj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-14-2019, 06:58 AM
  4. [SOLVED] Power Pivot does not load new column added in Power query
    By ibuhary in forum Excel General
    Replies: 12
    Last Post: 02-19-2019, 03:53 AM
  5. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  6. [SOLVED] Power Query to Extract Data based on multiple criteria
    By Philipsfn in forum Excel Formulas & Functions
    Replies: 27
    Last Post: 01-16-2018, 01:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1