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
ctaylor
Helper III
Helper III

Creating a Calculated Table for Item and Measure for All Months in Date Table.

Hello,

 

I am looking to make a computed table using a date table and a list of properties that shows the occupancy % for each property and each month. 

 

The table would essentially look like this:

ctaylor_0-1596637934715.png

 



I have tried what I know to pivot/unpivot data in the two tables but I can't figure out how to essentially duplicate the calendar month for every property in the list (about 200). 

 

What the best way to create this summary table?

Thanks! 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

I think you will need to use a Cross Join trick in Power Query where you basically drop one table into a new column, then expand.

This code will create a table of 12 months all using the first of the month. Jan 1, 2020, Feb 1, 2020, etc..

 

let
    Source = Table.FromList({1..12}, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
    #"Convert to Date" = Table.TransformColumns(Source, {{"Date", each #date(2020,_,1), type date}})
in
    #"Convert to Date"

 

 Then do the following:

edhans_0-1596643209598.png

  1. This is the Date query created with the M code above
  2. Add a new custom column and simply reference the Date query. Press ok.
  3. You will have a Date column that has Table in yellow. If you click in the white area next to the word table you can see...
  4. all of the dates
  5. click the Expand button and you get the following. All 12 dates in every single Property line.

edhans_1-1596643351289.png

 

If you want to get fancy, you could do all of that in your main table without a new query. The M code is a bit tedious to type in and harder to follow, but same result.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmAyEUwmgclkpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Property = _t]),
    #"Added Date Column" = 
        Table.AddColumn(
            Source, "Date", 
            each 
                Table.TransformColumns(
                        Table.FromList({1..12}, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
                        {{"Date", each #date(2020,_,1), type date}}
                    )
                ),
    #"Expanded Date" = Table.ExpandTableColumn(#"Added Date Column", "Date", {"Date"}, {"Date"})
in
    #"Expanded Date"

The #"Added Date Column" is the relevant step.

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

I think you will need to use a Cross Join trick in Power Query where you basically drop one table into a new column, then expand.

This code will create a table of 12 months all using the first of the month. Jan 1, 2020, Feb 1, 2020, etc..

 

let
    Source = Table.FromList({1..12}, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
    #"Convert to Date" = Table.TransformColumns(Source, {{"Date", each #date(2020,_,1), type date}})
in
    #"Convert to Date"

 

 Then do the following:

edhans_0-1596643209598.png

  1. This is the Date query created with the M code above
  2. Add a new custom column and simply reference the Date query. Press ok.
  3. You will have a Date column that has Table in yellow. If you click in the white area next to the word table you can see...
  4. all of the dates
  5. click the Expand button and you get the following. All 12 dates in every single Property line.

edhans_1-1596643351289.png

 

If you want to get fancy, you could do all of that in your main table without a new query. The M code is a bit tedious to type in and harder to follow, but same result.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmAyEUwmgclkpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Property = _t]),
    #"Added Date Column" = 
        Table.AddColumn(
            Source, "Date", 
            each 
                Table.TransformColumns(
                        Table.FromList({1..12}, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
                        {{"Date", each #date(2020,_,1), type date}}
                    )
                ),
    #"Expanded Date" = Table.ExpandTableColumn(#"Added Date Column", "Date", {"Date"}, {"Date"})
in
    #"Expanded Date"

The #"Added Date Column" is the relevant step.

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

@edhans 

Thank you!

 

I didn't need to go with the fancy code at the bottom, just the concept of the first section was what I needed.  I had done this before for another reason but I couldn't remember what to do to make the whole of the calendar apply to each property.

 

Excellent @ctaylor - glad I was able to help out.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.

Top Solution Authors
Top Kudoed Authors