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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
curious_monkey
Regular Visitor

Power BI DAX Measure to create a view of Weekly Sales Count

Hi Guys, need help in writing a DAX query to create a Table view in Power BI

DAXTable.JPG

As you can see above, I have a Table - which has cities and dates with Sales count, I want to create a view with single record per city and sales count of all its occurrences in that week ending on Saturday.

So far i have written this ('Data Sheet' is the name of the excel I am reading data from ...)

 

WeeklySalesCount = 
ADDCOLUMNS(
    GENERATE(
        DataSheet,
        DATESBETWEEN(
            CALENDAR(MIN(DataSheet[Date]), MAX(DataSheet[Date])),
            [Date] - WEEKDAY([Date], 2) + 1,
            [Date] - WEEKDAY([Date], 2) + 7
        )
    ),
    "Location", InvIssue[Location],
    "Sales", InvIssue[Sales]
)

but power gives me error saying, it cannot find this column date: Any other idea, as how to create this table using a DAX measure???

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1682046991273.png

 

 

New Table = 
VAR _wknumber =
    ADDCOLUMNS ( Data, "@wknumber", WEEKNUM ( Data[Date], 1 ) )
VAR _wkendingdate =
    ADDCOLUMNS (
        _wknumber,
        "@wkendingdate", MAXX ( FILTER ( _wknumber, [@wknumber] = EARLIER ( [@wknumber] ) ), Data[Date] )
    )
RETURN
    GROUPBY (
        _wkendingdate,
        Data[Location],
        [@wkendingdate],
        "@Sales", SUMX ( CURRENTGROUP (), Data[Sales] )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

2 REPLIES 2
ThxAlot
Super User
Super User

Bizarre that your 1 Apr, 2023 is Sunday ...

 

Som Weekly.pbix

Anyway, it's easy enough to solve by PQ

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddLLDcMgDAbgXThHKjamwLEpW0TZf42mtPIjhhv6ZP2yjY8j7GEL8Ij0wIjpeqcUzu0I/cYIg7/VqKvblBHn/Kt+XU/SHAe/71y4OiumyNl53rdmTJzddDVxNkTtRaaPfs7hZi2Zm4Hkc7wTt2P8v4Lu6rM4+WlHjhm3LjzJvE/vu/MmOcZJ+pnUj/zi++l3T/K3ULXDwuVErJeFqyuui7mav7XuPHM/aO6hLlzyjav9I8znNa7+BSf31p1f+zk/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Loc = _t, Date = _t, Qty = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Loc", type text}, {"Date", type date}, {"Qty", Int64.Type}}),
    Sorted = Table.Sort(#"Changed Type",{{"Loc", Order.Ascending}, {"Date", Order.Ascending}}),

    #"Grouped by Week" = Table.Group(Sorted, {"Loc","Date"}, {"Grp", each [Weekly = List.Sum([Qty]), Saturday = Date.ToText(Date.EndOfWeek([Date]{0},Day.Sunday), "ddd dd/MM/yyyy")]}, 0, (x,y) => Byte.From(Date.WeekOfYear(x[Date],Day.Sunday)<>Date.WeekOfYear(y[Date],Day.Sunday))),
    #"Expanded Grp" = Table.RemoveColumns(Table.ExpandRecordColumn(#"Grouped by Week", "Grp", {"Saturday","Weekly"}), "Date")
in
    #"Expanded Grp"

ThxAlot_1-1682068636536.png

 

Equally easy by DAX

ThxAlot_2-1682068752978.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1682046991273.png

 

 

New Table = 
VAR _wknumber =
    ADDCOLUMNS ( Data, "@wknumber", WEEKNUM ( Data[Date], 1 ) )
VAR _wkendingdate =
    ADDCOLUMNS (
        _wknumber,
        "@wkendingdate", MAXX ( FILTER ( _wknumber, [@wknumber] = EARLIER ( [@wknumber] ) ), Data[Date] )
    )
RETURN
    GROUPBY (
        _wkendingdate,
        Data[Location],
        [@wkendingdate],
        "@Sales", SUMX ( CURRENTGROUP (), Data[Sales] )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.