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
zwyatt
Frequent Visitor

How to Convert New Sales Territory/Rep Data Table with Single Date "Event" Rows

Hitting brick wall and reaching out while trying to fully convert a new Territory / Salesperson Assignment table in Power Query.  

 

Was given a new SQL "Territory Assignment" table only containing initial 'event' rows with single date (for each particular Territory / SalesRep combination), such as when a territory is first assigned a rep OR if a new rep replaces another. 

- If a territory has had only one rep (like Territory 1), then there is only one initial date row.  

I'm needing a way to modify this single date 'event' row table by inserting add'l rows for every date (for each territory/salesrep combo) after or between what's given.  

 

**  This is an over-simplified example of what I'm needing to accomplish, for the actual table. 


A few "example" rows of the current report showing the 2 types of territory/rep 'events':

 

TerritoryIdEmployeeIdStartDate
13456/1/2025
17896/5/2025
245456/3/2025

 

I'm needing a way to CONVERT that format, into this:

 

TerritoryIdEmployeeIdStartDate
13456/1/2025
13456/2/2025
13456/3/2025
13456/4/2025
17896/5/2025
17896/6/2025
17896/7/2025
17896/8/2025
17896/9/2025
17896/10/2025
245456/3/2025
245456/4/2025
245456/5/2025
245456/6/2025
245456/7/2025
245456/8/2025
245456/9/2025
245456/10/2025

 

The solution would be to generate add'l row for each day between start and replace dates, and IF only ONE rep has been assigned, then a row for each date through yesterday.  

 

** Here is a sample blank PBI report with a data table containing large sample portion of the new table.

TerritoryEmployeeDate Example.pbix 

1 ACCEPTED SOLUTION
v-pnaroju-msft
Community Support
Community Support

Thank you, @ZhangKun, @SundarRaj and @ronrsnfld, for your responses.

Hi @zwyatt,

We would like to check if the solution provided by @ZhangKun, @SundarRaj, @ronrsnfld has resolved your issue. If you have found an alternative approach, we encourage you to share it with the community to assist others facing similar challenges.
If you found the response helpful, please mark it as the accepted solution. This recognition benefits other members seeking solutions to similar queries.

Thank you.

View solution in original post

6 REPLIES 6
v-pnaroju-msft
Community Support
Community Support

Thank you, @ZhangKun, @SundarRaj and @ronrsnfld, for your responses.

Hi @zwyatt,

We would like to check if the solution provided by @ZhangKun, @SundarRaj, @ronrsnfld has resolved your issue. If you have found an alternative approach, we encourage you to share it with the community to assist others facing similar challenges.
If you found the response helpful, please mark it as the accepted solution. This recognition benefits other members seeking solutions to similar queries.

Thank you.

 

Hey guys. Apologies for the delay.  

Yes! The original solution posted by @ZhangKun worked!  All I had to adjust was the End Date variable from a static date, to a dynamic pull for Prior Day's date.  
Also, a few minutes of trial and error to swap in my actual data source and add'l columns.  

Other than those, please consider it solved.  

I haven't had a chance to review the other submissions, besides a cursory "skim" over, but will dig into them today, to see if there's improvement in elegance or efficiency.  

Thanks again!

ronrsnfld
Super User
Super User

Another Method:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI2MQWSZvqG+kYGRqZKsToQYXMLS7CwKULYCChgYgpVbgwVjwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TerritoryId = _t, EmployeeId = _t, StartDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"TerritoryId", Int64.Type}, {"EmployeeId", Int64.Type}, {"StartDate", type date}}),

//Could be today or any other date
/* Because of the subtraction to get Duration.Days, setting this
    as today will result in the proper calculations below*/
    Yesterday=Date.From(DateTime.FixedLocalNow()),

#"Grouped Territory" = Table.Group(#"Changed Type", {"TerritoryId"}, {
    {"Dates", (t)=> [a=List.Generate(
                        ()=>[a=List.Dates(t[StartDate]{0},
                                        Duration.Days(
                                            ((t[StartDate]{1}?)??Yesterday)-
                                            t[StartDate]{0}),
                                        #duration(1,0,0,0)),idx=0],
                        each [idx] < Table.RowCount(t),
                        each [a=List.Dates(t[StartDate]{[idx]+1},
                                        Duration.Days(
                                            ((t[StartDate]{[idx]+2}?)??Yesterday)-
                                            t[StartDate]{[idx]+1}),
                                        #duration(1,0,0,0)),idx=[idx]+1],
                        each[a]),
                     b=Table.FromColumns(
                          {t[EmployeeId]}
                         & {a}, type table[EmployeeId=Int64.Type,StartDate={date}]),
                     c= Table.ExpandListColumn(b,"StartDate")][c],
                            type table[EmployeeId=Int64.Type, StartDate=date]
                  }}),
    #"Expanded Dates" = Table.ExpandTableColumn(#"Grouped Territory", "Dates", {"EmployeeId", "StartDate"})
in
    #"Expanded Dates"

 

Source

ronrsnfld_0-1749814002877.png

 

Results

ronrsnfld_1-1749814024164.png

 

 

 

SundarRaj
Super User
Super User

Hi @zwyatt, another solution I could work out using Power Query. Thanks. I'll attach the file link. Have a look if that is what you intend to do with your query.
https://docs.google.com/spreadsheets/d/1a3q1IxlqHrM_Jgajq6B0zOkyni2vLFV9/edit?usp=sharing&ouid=10475...

Sundar Rajagopalan
ZhangKun
Super User
Super User

You can adjust dt to modify it to your desired date, or a dynamic date.

Very much appreciate the quick turnaround @ZhangKun !  I'll be back at my machine this afternoon to review what you put together and confirm the fix. 

Thanks!

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.

Top Solution Authors