Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
joshcomputer1
Helper V
Helper V

Help converting DAX to M

 

I have a calculation that I need to move to the query level.  The formula works fine where it is but I need to manipulate the data into groupings later in the process.  Anyone know how to do this in M.  

 

All the formula does is gets the sum of the calls column by hour and date. 

Calls Per Hour = calculate(sum('table1'[Calls]), allexcept('table1', table1[date], table1[hour]))

 

 

ss1.PNG

1 ACCEPTED SOLUTION

Hi @joshcomputer1,

 

1. I created the table SampleData using Enter Data

2. Using Edit Query did the following

         a) Group by Date/Hour create sum (RawCalls) and in the aggregation used AllRows

         b) Expanded the Grouped output Table.

        c) Removed unwanted columns

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjA0MLJR0lQyA2AtGmBkqxOlgkjUG0GQ5JMDbBIWkCxKYQKSN9IELTZ4RFzgiKQY6JBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Hour = _t, Calls = _t, ASA = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Hour", Int64.Type}, {"Calls", Int64.Type}, {"ASA", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Hour"}, {{"Sum", each List.Sum([Calls]), type number}, {"AllRows", each _, type table}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Date", "Hour", "Calls", "ASA"}, {"AllRows.Date", "AllRows.Hour", "AllRows.Calls", "AllRows.ASA"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded AllRows",{"AllRows.Date", "AllRows.Hour"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Sum", "TotalCalls"}, {"AllRows.Calls", "Raw Calls"}, {"AllRows.ASA", "ASA"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"ASA", Int64.Type}, {"Raw Calls", Int64.Type}, {"TotalCalls", Int64.Type}})
in
    #"Changed Type1"

 

 

3. Created the following measures

     %TotalCalls = SUMX(SampleData,DIVIDE([Raw Calls],[TotalCalls]))

 

      AdjustedASA = SUMX(SampleData,[%TotalCalls] *[ASA] )

 

4. Created a table visual sample output attached.

 

If this works for you please accept as solution and also give KUDOS.

 

Cheers

 

CheenuSing

 

Capture.JPG

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

View solution in original post

9 REPLIES 9
v-frfei-msft
Community Support
Community Support

Hi @joshcomputer1,

 

We can use group by function in Power query to achieve your goal.

 

Capture.PNG

2.PNG

Here is the M code for your reference.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMLRQ0lEyBGJjpVgdDEFTdEEjIDaBChohqzTCJmiMLgjVHgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Hour = _t, Calls = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Hour", Int64.Type}, {"Calls", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Hour", "Date"}, {{"Count", each List.Sum([Calls]), type number}})
in
    #"Grouped Rows"


For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Hi, 

 

I assume that you want to have the calculated column instead of a summarized table, which is easier. Here is the M language which can create the same calcualted column in DAX:

 

let
  Source = ..... (your source)
  Table6_Table = Source{[Item="Table6",Kind="Table"]}[Data],
  #"Changed Type" = Table.TransformColumnTypes(Table6_Table,{{"Date", type date}, {"Hours", Int64.Type}, {"Calls", Int64.Type}}),

 

  //Add merge column: concatnate date and hour
  Table_1 = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({Text.From([Date], "en-US"), Text.From([Hours],                          "en-US")}, " "), type text),

  //Create summarized table
  #"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Hours"}, {{"Total Calls", each List.Sum([Calls]), type number}}),
  Table_2 = Table.AddColumn(#"Grouped Rows", "Merged", each Text.Combine({Text.From([Date], "en-US"), Text.From([Hours],                       "en-US")}, " "), type text),

  //Merge 2 tables, expand and delete merge column
  #"Merge 2 tables" = Table.NestedJoin(Table_1,{"Merged"},Table_2,{"Merged"},"Table 2",JoinKind.LeftOuter),
  #"Expanded Table 2" = Table.ExpandTableColumn(#"Merge 2 tables", "Table 2", {"Total Calls"}, {"Total Calls"}),
  #"Removed Columns" = Table.RemoveColumns(#"Expanded Table 2",{"Merged"})
in
  #"Removed Columns"

 

What this code does:

  1. Create a summarized table by Date and Hr
  2. Add Concanate (Date + HR) in original table and summarized table
  3. Merge and expand total hr

Result:
Q23.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I hope this help.

 

 

 

 

 

LivioLanzo
Solution Sage
Solution Sage

Hello @joshcomputer1,

 

you can easily do this by using the GROUP BY feature. Watch this

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

I cannot do the group by yet. AFter this column is calc'ed, I will add another column for a calculation then do a group by.  I need to do this in m.

In M it is possible to GROUP BY and then expand the column. If you take a look at the video link I posted you can see it

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

What I need to do is a bit more complex. The video is helpful though. 

I need to group by day and hour, but I need to divide by the total number of calls for the hour.

 

Hour, Calls, and ASA are all data fields, the rest are calcs. 

 

Total calls per hour = sum of all calls during the hour

% of total calls = the percentage of calls on that row of the total for the hour

adjusted ASA= [% of total calls] * [ASA]

 

ss1.PNG

 

Hi @joshcomputer1,

 

1. I created the table SampleData using Enter Data

2. Using Edit Query did the following

         a) Group by Date/Hour create sum (RawCalls) and in the aggregation used AllRows

         b) Expanded the Grouped output Table.

        c) Removed unwanted columns

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjA0MLJR0lQyA2AtGmBkqxOlgkjUG0GQ5JMDbBIWkCxKYQKSN9IELTZ4RFzgiKQY6JBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Hour = _t, Calls = _t, ASA = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Hour", Int64.Type}, {"Calls", Int64.Type}, {"ASA", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Hour"}, {{"Sum", each List.Sum([Calls]), type number}, {"AllRows", each _, type table}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Date", "Hour", "Calls", "ASA"}, {"AllRows.Date", "AllRows.Hour", "AllRows.Calls", "AllRows.ASA"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded AllRows",{"AllRows.Date", "AllRows.Hour"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Sum", "TotalCalls"}, {"AllRows.Calls", "Raw Calls"}, {"AllRows.ASA", "ASA"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"ASA", Int64.Type}, {"Raw Calls", Int64.Type}, {"TotalCalls", Int64.Type}})
in
    #"Changed Type1"

 

 

3. Created the following measures

     %TotalCalls = SUMX(SampleData,DIVIDE([Raw Calls],[TotalCalls]))

 

      AdjustedASA = SUMX(SampleData,[%TotalCalls] *[ASA] )

 

4. Created a table visual sample output attached.

 

If this works for you please accept as solution and also give KUDOS.

 

Cheers

 

CheenuSing

 

Capture.JPG

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

That worked. I am not fully sure why, but it works. I was lost at the expanded rows part. 

Anonymous
Not applicable

Goto Edit Queries :

Capture.PNG

 

 

 

and click on Group by then goto --> Advanced and add Date and Hours in group by aggregation as  "Sum "  and total as columns.

 

Hope u got it 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.