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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
wmf_07
Frequent Visitor

Create a new table to obtain daily target

I have a table with monthly targets by branch and category for each month. Is it possible to create a table that includes all the month dates with its daily target by branch and category? 

 

Daily Target = Target / No.of days in that month.

 

The reason I want it as a table and not a measure is because in my sales data when the agent has not made a sale on 1 day it does not show his daily sale vs daily target. eg. if the agent has not made a sale on 3rd Jan in category A it does not show Daily sale vs target ( 0 vs 26)

 

The data I have :

 

MonthBranchCategoryMonthly Target
Jan AXA800
Jan BCB500
FebAXA750
FebBCB550

 

Desired Output :

 

DateBranchCategoryDaily Target
01-01-2024AXA26
02-01-2024AXA26
03-01-2024AXA26
04-01-2024AXA26
05-01-2024AXA26
06-01-2024AXA26
07-01-2024AXA26
08-01-2024AXA26
09-01-2024AXA26
10-01-2024AXA26
11-01-2024AXA26
12-01-2024AXA26
13-01-2024AXA26
14-01-2024AXA26
15-01-2024AXA26
16-01-2024AXA26
17-01-2024AXA26
18-01-2024AXA26
19-01-2024AXA26
20-01-2024AXA26
21-01-2024AXA26
22-01-2024AXA26
23-01-2024AXA26
24-01-2024AXA26
25-01-2024AXA26
26-01-2024AXA26
27-01-2024AXA26
28-01-2024AXA26
29-01-2024AXA26
30-01-2024AXA26
31-01-2024AXA26
2 ACCEPTED SOLUTIONS
Alex87
Solution Supplier
Solution Supplier

Hello,

You need your months to be Dates. You can use the following M code :

 

let
 Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3PK8lQ0lFyKkrMSwYxnBNLUtPziyqBTLBcTqVCSGJRemqJUqxOtJKBoT4QGRkYmQDlHSNABBBbGBhgyjo5gwggNkWSNcLQa26KRRahFyQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Month", type date}, {"Monthly Target", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Month", "StartDate"}}),
    EndDate = Table.AddColumn(#"Renamed Columns", "EndDate", each Date.EndOfMonth([StartDate]), type date),
    List = Table.TransformColumnTypes(
Table.AddColumn(
            Table.AddColumn(EndDate, "Date", each { Number.From([StartDate])..Number.From([EndDate]) } 
            ), "Temp", each List.Count( [Date] ) 
        ),
{{"Temp", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(List, "MonthlyTargt", each [Monthly Target]/[Temp]),
    ChType = Table.TransformColumnTypes(#"Added Custom",{{"MonthlyTargt", Int64.Type}}),
    Scope = Table.SelectColumns(ChType,{"Branch", "Category", "Date", "MonthlyTargt"}),
    ExpandDates = Table.TransformColumnTypes( Table.ExpandListColumn(Scope, "Date"), {{"Date", type date}}) //Expand and assign type to the date column
in
    ExpandDates

 

Alex87_0-1714825306670.png

 

If it answers your query, please mark my reply as the solution. Thanks!

View solution in original post

Hi @wmf_07 

There are many ways to achive your requried output and one of them is creating a crossjoin of your table and calendar table and filtering out the non-existent data rows in the combined table. 

First, crossjoin the calendar table with day granularity and your table with monthly target.  

DataNinja777_0-1714825882104.png

Then, if there is no monthly target data in the original table, delete the rows in the 2nd calculated table.  

DataNinja777_1-1714825993492.png

Your required output is produced in 'Table 2' as shown below:

DataNinja777_2-1714826387132.png

 

I attach the example pbix file.  The above method is probably involving more steps than what is possible and there maybe more simple and elegant ways to do it using dax, but please let me know if this meets your requirement. 

Best regards,

 

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Branch", type text}, {"Category", type text}, {"Monthly Target", Int64.Type}}),
    #"Inserted Days in Month" = Table.AddColumn(#"Changed Type", "Daily target", each [Monthly Target]/Date.DaysInMonth([Month]), type number),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Days in Month",{"Monthly Target"}),
    #"Inserted End of Month" = Table.AddColumn(#"Removed Columns", "Date", each {Number.From([Month])..Number.From(Date.EndOfMonth([Month]))}),
    #"Expanded List" = Table.ExpandListColumn(#"Inserted End of Month", "Date"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded List",{{"Date", type date}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"Month"})
in
    #"Removed Columns1"

Hope this helps.

Ashish_Mathur_0-1714866927624.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Alex87
Solution Supplier
Solution Supplier

Hello,

You need your months to be Dates. You can use the following M code :

 

let
 Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3PK8lQ0lFyKkrMSwYxnBNLUtPziyqBTLBcTqVCSGJRemqJUqxOtJKBoT4QGRkYmQDlHSNABBBbGBhgyjo5gwggNkWSNcLQa26KRRahFyQbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Month", type date}, {"Monthly Target", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Month", "StartDate"}}),
    EndDate = Table.AddColumn(#"Renamed Columns", "EndDate", each Date.EndOfMonth([StartDate]), type date),
    List = Table.TransformColumnTypes(
Table.AddColumn(
            Table.AddColumn(EndDate, "Date", each { Number.From([StartDate])..Number.From([EndDate]) } 
            ), "Temp", each List.Count( [Date] ) 
        ),
{{"Temp", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(List, "MonthlyTargt", each [Monthly Target]/[Temp]),
    ChType = Table.TransformColumnTypes(#"Added Custom",{{"MonthlyTargt", Int64.Type}}),
    Scope = Table.SelectColumns(ChType,{"Branch", "Category", "Date", "MonthlyTargt"}),
    ExpandDates = Table.TransformColumnTypes( Table.ExpandListColumn(Scope, "Date"), {{"Date", type date}}) //Expand and assign type to the date column
in
    ExpandDates

 

Alex87_0-1714825306670.png

 

If it answers your query, please mark my reply as the solution. Thanks!

Hi @wmf_07 

There are many ways to achive your requried output and one of them is creating a crossjoin of your table and calendar table and filtering out the non-existent data rows in the combined table. 

First, crossjoin the calendar table with day granularity and your table with monthly target.  

DataNinja777_0-1714825882104.png

Then, if there is no monthly target data in the original table, delete the rows in the 2nd calculated table.  

DataNinja777_1-1714825993492.png

Your required output is produced in 'Table 2' as shown below:

DataNinja777_2-1714826387132.png

 

I attach the example pbix file.  The above method is probably involving more steps than what is possible and there maybe more simple and elegant ways to do it using dax, but please let me know if this meets your requirement. 

Best regards,

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.