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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Dukezera
Frequent Visitor

Help expanding table according dates

I have a table like below, where a new record is created when there is a change in the status of a task.


taskstatlastupdate
A128/04/2022
A301/05/2022
A505/05/2022
B128/04/2022
B303/05/2022
B405/05/2022

The problem is that I need to plot a graph within a time range, where I know the status of each item regardless of the date it was changed/created. With that, I think the easiest is to transform to the table below:

 


taskstatuslastupdate
A128/04/2022
A129/04/2022
A128/04/2022
A129/04/2022
A130/04/2022
A301/05/2022
A302/05/2022
A303/05/2022
A304/05/2022
A505/05/2022
B128/04/2022
B129/04/2022
B130/04/2022
B101/05/2022
B102/05/2022
B303/05/2022
B304/05/2022
B405/05/2022

However, I can't think of a way to do it, either directly in Power BI or even in SQL, since I'm connecting to a redshift database through a sql query. Could you please help me? Thank you

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@Dukezera Try:

Tasks2 = 
    ADDCOLUMNS(
        GENERATE(
            DISTINCT('Tasks'[task]),
            CALENDAR(MIN('Tasks'[lastupdate]),MAX('Tasks'[lastupdate]))
        ),
        "status",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stat])
    )

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

@Dukezera Great! So I don't know where those columns are coming from exactly or how they work but seems like you could do something like:

Tasks2 = 
VAR __Table =
    ADDCOLUMNS(
        GENERATE(
            DISTINCT('Tasks'[task]),
            CALENDAR(MIN('Tasks'[lastupdate]),MAX('Tasks'[lastupdate]))
        ),
        "status",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stat]),
        "approved",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[approved]),
        "stn",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stn])
    )
RETURN
  FILTER(__Table, [status] <> BLANK() && [approved] <> BLANK() && [stn] <> BLANK())

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@Dukezera Try:

Tasks2 = 
    ADDCOLUMNS(
        GENERATE(
            DISTINCT('Tasks'[task]),
            CALENDAR(MIN('Tasks'[lastupdate]),MAX('Tasks'[lastupdate]))
        ),
        "status",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stat])
    )

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

I tried but got this error:
The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.

@Dukezera What I provided is a calculated table formula.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler works perfectly!!!!!!!

What do I need to do with I have a table like this (with two more column and works like status) and also how to remove rows with blank values in any of this columns (status, approved or stn)?:

 

taskstatuslastupdateapprovedstn
A128/04/2022NN31
A301/05/2022NN31
A505/05/2022YN31
B128/04/2022NN31
B303/05/2022YN31
B405/05/2022YN31

@Dukezera Great! So I don't know where those columns are coming from exactly or how they work but seems like you could do something like:

Tasks2 = 
VAR __Table =
    ADDCOLUMNS(
        GENERATE(
            DISTINCT('Tasks'[task]),
            CALENDAR(MIN('Tasks'[lastupdate]),MAX('Tasks'[lastupdate]))
        ),
        "status",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stat]),
        "approved",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[approved]),
        "stn",
                VAR __Date = [Date]
                VAR __Task = [task]
                VAR __TargetDate = MAXX(FILTER('Tasks',[lastupdate] <= __Date && [task] = __Task),[lastupdate])
            RETURN
                MAXX(FILTER('Tasks',[task] = __Task && [lastupdate] = __TargetDate),[stn])
    )
RETURN
  FILTER(__Table, [status] <> BLANK() && [approved] <> BLANK() && [stn] <> BLANK())

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.