Permutations table in Power BI or Power Query in Excel

Today my husband came with a special task to me: He had to re-wire an old instrument on board of our sailing boat and “lost” the original matching instructions. There were 4 cables to go into 4 different holes, which left him with 24 possible combinations. So he needed a permutations table with all possible combinations that he could print out and tick each combination that he would try subsequently:

To be honest, he was quicker in creating the permutations table manually in Excel than me in Power Query, as it took me a while to develop an easy enough algorithm. But the technique I came out with at the end is so typical “Power Query”-ish, that I’m going to share it here:

The Video

The Function

Here comes the function code:

Enjoy & stay queryious 😉

Comments (11) Write a comment

  1. Table.FromColumns auto-generates column names which simplifies things a bit. List.Accumulate can also be used instead. So another way is:

    (List as list) =>
    Table = Table.FromColumns(List.Repeat({{List}},List.Count(List)))
    ,Expand = List.Accumulate(Table.ColumnNames(Table),Table,(a,b) => Table.ExpandListColumn(a, b))


    • Thanks Cameron!
      Yours is a very nice example of a variation to my task, as yours can contain one item multiple times.
      So in the example of 4 list items, it produces 256 rows/possible combinations, while mine only returns 24.
      Very nice trick with the dynamic expansion!


    • Agree with the suggestion of List.Aggregate (less code and possibly a bit less memory as well, although I don’t know how the effect of lazy evaluation actually is here).
      Have adjusted the code above, so thanks again Cameron!


      • I was a bit fast off the mark with my solution. Yes, it’s a bit of a complex problem. Cool stuff though.


  2. This is incredible because i had exactly the same problem 2 or 3 years ago.
    Of course, like you, i started to think how to resolve it in PQ. My solution was based exactly on the same logic as yours. The only difference is that i used the table as an argument for function and i used recursion.
    Out of curiosity, I checked both solutions for nine colors (362 880 rows as result) – the execution time is almost identical (maybe some tenths of a second of difference).
    Nice to know that we think alike :-))


  3. let
    源 = Web.Page(”
    var oExcel,oWorkbook,oSheet,oRange,strAllName
    oExcel = new ActiveXObject(‘Excel.Application’);
    oRange,”,’’,’Power Query’,’Power Query中文博客’)
    源 //在桌面畅心工作簿的第一个表的A1单元格插入超链接


    • Hey, that looks like VBA 🙂
      But unfortunately it’s not working for me. Played around with the syntax a bit, but couldn’t get it working.
      Any idea what could be the reason? What modifications did you do to the script from the VBA-editor?
      Thanks and cheers, Imke


  4. First, you need to set up the IE browser,
    Enable ActiveX control initialization and running options for untagged executable scripts.
    Then our Power Query can be used like VBA.


Leave a Reply

Die Datenschutzbestimmungen finden Sie hier: / Please find the privacy policy here: