PowerQuery: taking the average of each of many columns











up vote
0
down vote

favorite
1












I'm new to PowerQuery and I have a table that is essentially a matrix of dates and hours within those days: the first column holds each date and the rest of the columns are labeled 1 through 24. An example is:



Date   H1   H2   H3   H4 ...
---- -- -- -- --
Jan 1
Jan 2
Jan 3
...


This is stored in an Excel file that is quite large, so I want to be able to simply query that file and pull subsets of the data. One example is the average hourly number by year. In SQL this would be represented by "SELECT YEAR(Date), AVG(H1), AVG(H2), ... FROM Source Table GROUPBY YEAR(Date)". However, in PowerQuery it seems like you can only use GROUPBY to generate a new column with the grouped result and thus have to repeat the operation x24 in this case, or more if I had data by seconds for example (to be fair, in the SQL query you also have to type out each column if you don't consider scripting solutions). Is there a simpler approach to generate my desired table (essentially collapsing each column to its average), or do I need to manually add each column?










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I'm new to PowerQuery and I have a table that is essentially a matrix of dates and hours within those days: the first column holds each date and the rest of the columns are labeled 1 through 24. An example is:



    Date   H1   H2   H3   H4 ...
    ---- -- -- -- --
    Jan 1
    Jan 2
    Jan 3
    ...


    This is stored in an Excel file that is quite large, so I want to be able to simply query that file and pull subsets of the data. One example is the average hourly number by year. In SQL this would be represented by "SELECT YEAR(Date), AVG(H1), AVG(H2), ... FROM Source Table GROUPBY YEAR(Date)". However, in PowerQuery it seems like you can only use GROUPBY to generate a new column with the grouped result and thus have to repeat the operation x24 in this case, or more if I had data by seconds for example (to be fair, in the SQL query you also have to type out each column if you don't consider scripting solutions). Is there a simpler approach to generate my desired table (essentially collapsing each column to its average), or do I need to manually add each column?










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I'm new to PowerQuery and I have a table that is essentially a matrix of dates and hours within those days: the first column holds each date and the rest of the columns are labeled 1 through 24. An example is:



      Date   H1   H2   H3   H4 ...
      ---- -- -- -- --
      Jan 1
      Jan 2
      Jan 3
      ...


      This is stored in an Excel file that is quite large, so I want to be able to simply query that file and pull subsets of the data. One example is the average hourly number by year. In SQL this would be represented by "SELECT YEAR(Date), AVG(H1), AVG(H2), ... FROM Source Table GROUPBY YEAR(Date)". However, in PowerQuery it seems like you can only use GROUPBY to generate a new column with the grouped result and thus have to repeat the operation x24 in this case, or more if I had data by seconds for example (to be fair, in the SQL query you also have to type out each column if you don't consider scripting solutions). Is there a simpler approach to generate my desired table (essentially collapsing each column to its average), or do I need to manually add each column?










      share|improve this question













      I'm new to PowerQuery and I have a table that is essentially a matrix of dates and hours within those days: the first column holds each date and the rest of the columns are labeled 1 through 24. An example is:



      Date   H1   H2   H3   H4 ...
      ---- -- -- -- --
      Jan 1
      Jan 2
      Jan 3
      ...


      This is stored in an Excel file that is quite large, so I want to be able to simply query that file and pull subsets of the data. One example is the average hourly number by year. In SQL this would be represented by "SELECT YEAR(Date), AVG(H1), AVG(H2), ... FROM Source Table GROUPBY YEAR(Date)". However, in PowerQuery it seems like you can only use GROUPBY to generate a new column with the grouped result and thus have to repeat the operation x24 in this case, or more if I had data by seconds for example (to be fair, in the SQL query you also have to type out each column if you don't consider scripting solutions). Is there a simpler approach to generate my desired table (essentially collapsing each column to its average), or do I need to manually add each column?







      powerquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 11:04









      user3830878

      83




      83
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.



          enter image description here



          I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.



          enter image description here



          You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.



          let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          #"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
          #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
          in
          #"Grouped Rows"





          share|improve this answer





















          • This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
            – user3830878
            Nov 9 at 8:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206446%2fpowerquery-taking-the-average-of-each-of-many-columns%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.



          enter image description here



          I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.



          enter image description here



          You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.



          let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          #"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
          #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
          in
          #"Grouped Rows"





          share|improve this answer





















          • This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
            – user3830878
            Nov 9 at 8:15















          up vote
          0
          down vote



          accepted










          You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.



          enter image description here



          I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.



          enter image description here



          You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.



          let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          #"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
          #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
          in
          #"Grouped Rows"





          share|improve this answer





















          • This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
            – user3830878
            Nov 9 at 8:15













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.



          enter image description here



          I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.



          enter image description here



          You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.



          let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          #"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
          #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
          in
          #"Grouped Rows"





          share|improve this answer












          You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.



          enter image description here



          I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.



          enter image description here



          You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.



          let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          #"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
          #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
          #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
          in
          #"Grouped Rows"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 16:32









          Wedge

          1,119210




          1,119210












          • This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
            – user3830878
            Nov 9 at 8:15


















          • This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
            – user3830878
            Nov 9 at 8:15
















          This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
          – user3830878
          Nov 9 at 8:15




          This worked perfectly, I just developed a new appreciation for pivoting/unpivoting :) Thanks!
          – user3830878
          Nov 9 at 8:15


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206446%2fpowerquery-taking-the-average-of-each-of-many-columns%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff