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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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.LearnAndPractise(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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.