Results 1 to 1 of 1

Thread: PQ - Sort columns and rows

  1. #1
    Sir Moderator sandy666's Avatar
    Join Date
    May 2018
    Posts
    239
    Rep Power
    7

    Cool PQ - Sort columns and rows

    • rows are sorted ascending (A-Z)
    • columns are sorted ascending (A-Z) except first column which is TeamLeader's column
    • Person from the first top left cell is a Team Leader (in this case: Captain)
    • Team Leader's name can be changed and still will be in the first column (in this case: Captain)
    • code is squeezed as much as I could do that

    Source
    Budget Holding Positions Category
    Captain Capital Expense
    Captain Administration
    Captain Charter & Sale Related
    Engineer Comms & Subscription
    Chief Steward Crew
    Engineer Engineering Maintenance
    Engineer Fuel/Oil
    Bosum Hull & Deck Maintenance
    Chief Steward Interior Maintenance
    Captain Management
    Bosum Navigation
    Captain Payroll
    Captain Port Fees
    Bosum Safety & Security
    Bosum Tenders/Toys
    Captain Yacht Insurance
    Chief Steward Owner/Guest
    Engineer Refit
    Chief Steward VIP
    Bosum Asahi 2
    Bosum X-ray
    sandy666 Power Query
    vba
    Engineer A-bomb


    Result
    Captain Bosum Chief Steward Engineer sandy666
    A-bomb Asahi 2 Crew A-bomb Power Query
    Administration Hull & Deck Maintenance Interior Maintenance Comms & Subscription
    Asahi 2 Navigation Owner/Guest Engineering Maintenance
    Capital Expense Safety & Security VIP Fuel/Oil
    Charter & Sale Related Tenders/Toys Refit
    Comms & Subscription X-ray
    Crew
    Engineering Maintenance
    Fuel/Oil
    Hull & Deck Maintenance
    Interior Maintenance
    Management
    Navigation
    Owner/Guest
    Payroll
    Port Fees
    Power Query
    Refit
    Safety & Security
    Tenders/Toys
    VIP
    X-ray
    Yacht Insurance
    vba


    Code:
    // squeezed12
    let Source = Excel.CurrentWorkbook(){[Name="tblBdgtHoldgs"]}[Content],
        Category = Table.SelectColumns(Source,{"Category"}),
        Name = Table.AddColumn(Category, "Name", each Record.RemoveFields(Table.First(Source, {[Budget Holding Positions]}), "Category")),
        BHP = Table.TransformColumns(Table.ExpandRecordColumn(Name, "Name", {"Budget Holding Positions"}, {"Budget Holding Positions"}), {{"Budget Holding Positions", each " " & _, type text}}),
        Join = Table.Distinct(Table.Combine({BHP, Source})),
        TSR = Table.SelectRows(Join, each [Budget Holding Positions] <> List.First(Source[Budget Holding Positions]) and [Budget Holding Positions] <> null and [Category] <> null),
        Headers = List.Sort(List.Distinct(Table.Column(TSR, "Budget Holding Positions")), Order.Ascending),
        Pivot = Table.Pivot(TSR, Headers, "Budget Holding Positions", "Category", each List.Sort(_, Order.Ascending)),
            Function = (rec as record, fieldnames as list) =>
                let RTL = Record.ToList(Record.SelectFields(rec,fieldnames))
                in  Table.FromColumns(RTL,fieldnames)
    in  Table.AddColumn(Pivot, "Values", each Function(_, Headers)){0}[Values]
    Last edited by sandy666; 07-20-2020 at 10:38 PM.
    sandy
    I know you know but I forgot my Crystal Ball and don't know what you know also I may not remember what I did weeks, months or years ago so answer asap. Thinking doesn't hurt

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2014, 07:51 PM
  2. Replies: 8
    Last Post: 04-20-2014, 10:15 PM
  3. Replies: 18
    Last Post: 02-12-2014, 10:47 AM
  4. Replies: 6
    Last Post: 08-14-2013, 04:25 PM
  5. Replies: 2
    Last Post: 05-06-2011, 02:59 AM

Posting Permissions

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