Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Need help creating new column based on Category

Hello all, 

 

Very new to PowerBI and need help creating new columns based on couple criteria. Below is what my data looks like

SiteDateTypeSales
210/1/2024Coke50
109/30/2024Apple50
29/29/2024Apple125
19/29/2024Coke100
19/15/2024Pepsi500
29/1/2024Pepsi75
27/5/2024Coke75
16/30/2024Apple150
19/27/2024Pepsi200
29/30/2024Pepsi100
27/28/2024Apple225

 

 

What I need is I need sum of Column by type for last 7 days, last 30 days and last 365 days by site. Below is what my end goal should look like. Please help!!

SiteCokePepsiApple
1   
          7Day10020050
          30Day10070050
          365Day100700200
2   
          7Day50100125
          30Day50175125

          365Day

12575350

6 Replies

  • tackytechtom's avatar
    tackytechtom
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hi Anonymous ,

     

    How about this:

     

     

    Here the steps:

    1) create a date dimension that has the columns isLast7Days, isLast30Days and isLast365Days. Below some Power Query M code that creates such a table. But you can also google other ways of doing it. It's best practice to use Date/Calendar dimensions. If you do not know, how to exactly paste the M code into the advanced editor, please check out this quick walkthrough. 

    let
        // Define the start and end dates
        StartDate = #date(2024, 1, 1), // Change to your desired start date
        EndDate = DateTime.Date(DateTime.LocalNow()), // Current date
    
        // Generate a list of dates from StartDate to EndDate
        DateList = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1, 0, 0, 0)),
    
        // Convert the list to a table
        DateTable = Table.FromList(DateList, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
    
        // Add useful date fields
        AddYear = Table.AddColumn(DateTable, "Year", each Date.Year([Date]), Int64.Type),
        AddMonth = Table.AddColumn(AddYear, "Month", each Date.Month([Date]), Int64.Type),
        AddDay = Table.AddColumn(AddMonth, "Day", each Date.Day([Date]), Int64.Type),
        AddWeek = Table.AddColumn(AddDay, "Week", each Date.WeekOfYear([Date]), Int64.Type),
    
        // Add boolean fields for last 7, 30, and 365 days
        Today = Date.From(DateTime.LocalNow()),
        AddIsLast7Days = Table.AddColumn(AddWeek, "isLast7Days", each (if [Date] >= Date.AddDays(Today, -7) and [Date] <= Today then true else false)),
        AddIsLast30Days = Table.AddColumn(AddIsLast7Days, "isLast30Days", each (if [Date] >= Date.AddDays(Today, -30) and [Date] <= Today then true else false)),
        AddIsLast365Days = Table.AddColumn(AddIsLast30Days, "isLast365Days", each (if [Date] >= Date.AddDays(Today, -365) and [Date] <= Today then true else false)),
    
        // Add a field for Month Name and Year-Month for better reporting
        AddMonthName = Table.AddColumn(AddIsLast365Days, "MonthName", each Date.ToText([Date], "MMMM"), type text),
        AddYearMonth = Table.AddColumn(AddMonthName, "YearMonth", each Text.From([Year]) & "-" & Text.PadStart(Text.From([Month]), 2, "0"), type text),
        #"Changed Type" = Table.TransformColumnTypes(AddYearMonth,{{"Date", type date}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"Day", Int64.Type}, {"Week", Int64.Type}, {"isLast7Days", type logical}, {"isLast30Days", type logical}, {"isLast365Days", type logical}, {"MonthName", type text}, {"YearMonth", type date}})
    
    in
        #"Changed Type"

     

    2) Next create a relationship between the date dimension and your fact table. Make sure the connecting columns are having the same data type:

     

    3) Next create a calculation group and add elements like the below.

    7Day = CALCULATE(SELECTEDMEASURE(), 'Date'[isLast7Days] = TRUE() )

     

    You can create calculation groups in the modelling pane:

     

    Make sure that you are creating an explicit measure for your sales, like sales := SUM('Table'[Sales]). Using explicit measures is also best practice.

     

    4) Lastly, add the attributes, measures and calculation groups into a matrix visual like below:

     

     

    Let me know, if this helps 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello tackytechtom  Thank you so much for getting back. I'm stuck at step 4 where its giving me an error due to table can't determine the relationship between two or more fields. 

      I ensured both the datatype are same however the date column in my initial table obviously have same dates for different sites and its causing the relationship to me 'One to Many' Can you please help solving this issue or if you can share your example dashboard would be greatly appreciated. 

      • tackytechtom's avatar
        tackytechtom
        Icon for Most Valuable Professional rankMost Valuable Professional

        Hi Anonymous ,

         

        The relationship one to many (date -> fact table) is correct. I need to recreate the report. I'll come back to you tonight.

         

        /Tom

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you tackytechtom that worked perfect. I need your help again with adding couple more columns from a different table. Below is an example of the table


    Table2:

    SiteApple_goalCoke_goalPepsi_goal
    1212
    23.51.5
        

     

    Above are daily goals for those items. What I need to do is I need to first calculate goals for 7D, 30D, and 365D and bring them in along with the other columns so they are side by side and then calculate the different. See below the for the end result. Is this possible in PBI? Please help!!


     

    SiteCokeCoke_GoalDiffPepsiPepsi_GoalDiffAppleApple_GoalDiff
    1         
              7Day10079320014186501436
              30Day1003070700606405060-10
              365Day100365-265700730-30200730-530
    2         
              7Day503.546.510010.589.512521104
              30Day50153510090101259035

              365Day

    125182.5-57.5175547.5-372.53501095-745