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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
willb006
Regular Visitor

DATEADD using EARLIER function

Hello,

I am trying to get a solve for adding a date in this list using the previous date plus the days listed in the day column.  I tried the earlier function but it didn't work.  Can someone explain how I can do this as a measure?

 

willb006_0-1725910194703.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi,@willb006 ,thank you for your reply.
According to your latest information, you need to realize the effect of grouping calculation based on TicketID on the original basis, then you just need to change all the places in the code where ALL('Table') is used to ALLEXCEPT('Table'['ticket_number']), and let the original scope of the filter table which removes all the external filters be changed to just affected by the grouping of ['ticket_number'].
to achieve the effect of grouping (each ticket_number is counted internally).
like this:

vjtianmsft_0-1726019966922.png

result_column02 = 
VAR _date = [comment_sort]
VAR _numnotbalnk =
    CALCULATE (
        MAX ( 'Table'[comment_sort] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] < _date
                && NOT ( ISBLANK ( [wall_commentcreatedon] ) )
        )
    )
VAR diffday =
    CALCULATE (
        SUM ( 'Table'[Days] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] > _numnotbalnk
                && 'Table'[comment_sort] <= _date
        )
    )
VAR lastdate1 =
    CALCULATE (
        MAX ( 'Table'[wall_commentcreatedon] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] = _numnotbalnk
        )
    )
RETURN
    IF (
        ISBLANK ( 'Table'[wall_commentcreatedon] ),
        lastdate1 + diffday,
        'Table'[wall_commentcreatedon]
    )

Note that your data is quite specific and implements not really iterative logic (it looks like iteration, but it is not).
And fortunately, the source you gave has a comment_sort column to achieve an iteration-like effect (actually a conditional judgment based on sortnum, where each row is recalculated from the first row). If you don't provide a column for comment_sort, everything will get very tricky. (DAX itself doesn't support implementing iteration (it calculates uniformly based on a whole column, not on each cell, and there is no looping logic), and if the implementation wants true iteration logic, it's usually handled locally in the datasource or through Power Query using looping functions.
I hope my explanation can answer your questions.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
Kaviraj11
Super User
Super User

Hi, Can you provide an example with an expected output? 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Sure.  Here is an excel document with the expected output in the last column

 

ticket_numberwall_commentwall_commentcreatedoncomment_sortDaysExpected Output
6212482PENDING (CUST) PA REQUEST SUBMISSION - Assigned.

Assigned Role - Licensee.
6/8/2024 0:001106/8/2024
6212482PENDING SURVEY - Assigned.

Assigned Role - Owner.
6/17/2024 0:002406/17/2024
6212482PENDING (CUST) REVIEW - Assigned.

Assigned Role - Licensee.
9/4/2024 0:003109/4/2024
6212482PENDING (DOM) REVIEW - Assigned.
Assigned Role - Owner.
 4109/14/2024
6212482PENDING (DOM) DESIGN - Assigned.

Assigned Role - Owner.
9/6/2024 0:005109/6/2024
6212482PENDING (CUST) PAYMENT - Assigned.
Assigned Role - Owner.
 6109/16/2024
6212482PENDING Make-Ready - Assigned.
Assigned Role - Owner.
 71351/29/2025
Anonymous
Not applicable

Hello,Kaviraj11 ,thanks for your concern about this issue.
And I would like to share some additional solutions below.
Hi,@willb006  .I am glad to help you.

I have written the following code, and the end result is the same as your expected columns.

vjtianmsft_0-1725944294885.png

The Dax code(calculate column):

result_column02 = 
VAR _date = [comment_sort]
VAR _numnotbalnk =
    CALCULATE (
        MAX ( 'Table'[comment_sort] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[comment_sort] < _date
                && NOT ( ISBLANK ( [wall_commentcreatedon] ) )
        )
    )
VAR diffday =
    CALCULATE (
        SUM ( 'Table'[Days] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[comment_sort] > _numnotbalnk
                && 'Table'[comment_sort] <= _date
        )
    )
VAR lastdate1 =
    CALCULATE (
        MAX ( 'Table'[wall_commentcreatedon] ),
        FILTER ( ALL ( 'Table' ), 'Table'[comment_sort] = _numnotbalnk )
    )
RETURN
    IF (
        ISBLANK ( 'Table'[wall_commentcreatedon] ),
        lastdate1 + diffday,
        'Table'[wall_commentcreatedon]
    )

here is my test data:

vjtianmsft_1-1725944386961.png

Note that the effect of iterative looping is not possible with DAX functions in power BI Desktop (if you want to achieve this, it is generally recommended that you solve it at the data source or use a looping function through Power Query)
This is actually one of the flaws in Power BI DAX (because Power BI DAX code performs the same operation based on a whole column. Whereas excel can do this easily because it can use each cell as a unit of calculation)
Fortunately, your requirement implementation is not really iterative, so it can be achieved with dax code similar to iteration
I hope my test will help you.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.

Best Regards,

Carson Jian,

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-jtian-msft thank you for this.  Would it be iterative if I had more than one Ticket number?  I tried your solution and I had to do it like this " 

result_column02 =
VAR _date = SUM(Append1[comment_sort])
VAR _numnotbalnk =
    CALCULATE (
        SUM('Append1'[comment_sort] ),
        FILTER (
            ALL ( 'Append1'),
            'Append1'[comment_sort] < _date
                && NOT ( ISBLANK ( [wall_commentcreatedon] ) )
        )
    )
VAR diffday =
    CALCULATE (
        SUM ( 'Append1'[Days] ),
        FILTER (
            ALL ( 'Append1' ),
            'Append1'[comment_sort] > _numnotbalnk
                && 'Append1'[comment_sort] <= _date
        )
    )
VAR lastdate1 =
    CALCULATE (
        MAX ( 'Append1'[wall_commentcreatedon] ),
        FILTER ( ALL ( 'Append1' ), 'Append1'[comment_sort] = _numnotbalnk )
    )
RETURN
    IF (
        ISBLANK ( max('Append1'[wall_commentcreatedon] )),
        lastdate1 + diffday,
        max('Append1'[wall_commentcreatedon])
    )"  Here is the data set with more than one ticket
ticket_numberwall_commentwall_commentcreatedoncomment_sortDaysExpected Output
6212482PENDING (CUST) PA REQUEST SUBMISSION - Assigned.

Assigned Role - Licensee.
6/8/2024 0:001106/8/2024
6212482PENDING SURVEY - Assigned.

Assigned Role - Owner.
6/17/2024 0:002406/17/2024
6212482PENDING (CUST) REVIEW - Assigned.

Assigned Role - Licensee.
9/4/2024 0:003109/4/2024
6212482PENDING (DOM) REVIEW - Assigned.
Assigned Role - Owner.
 4109/14/2024
6212482PENDING (DOM) DESIGN - Assigned.

Assigned Role - Owner.
9/6/2024 0:005109/6/2024
6212482PENDING (CUST) PAYMENT - Assigned.
Assigned Role - Owner.
 6109/16/2024
6212482PENDING Make-Ready - Assigned.
Assigned Role - Owner.
 71351/29/2025
4979490PENDING (CUST) PA REQUEST SUBMISSION - Assigned.

Assigned Role - Licensee.
1/26/2022 0:001101/26/2022
4979490PENDING SURVEY - Assigned.

Assigned Role - Owner.
5/8/2023 0:002405/8/2023
4979490PENDING (CUST) REVIEW - Assigned.

Assigned Role - Licensee.
 3106/17/2023
4979490PENDING (DOM) REVIEW - Assigned.

Assigned Role - Owner.
 4106/27/2023
4979490PENDING (DOM) DESIGN - Assigned.

Assigned Role - Owner.
 5107/7/2023
4979490PENDING (CUST) PAYMENT - Assigned.

Assigned Role - Owner.
 6107/17/2023
4979490PENDING Make-Ready - Assigned.

Assigned Role - Owner.
5/8/2023 0:0071355/8/2023
4979490PENDING GRANTED - Assigned.

Assigned Role - Owner.
10/11/2023 0:008110/11/2023
4979490PENDING (CUST) PA WORK COMPLETE - Assigned.

Assigned Role - Licensee.
10/11/2023 0:009110/11/2023
4979490PENDING VALIDATE - Assigned.

Assigned Role - Owner.
8/26/2024 0:001018/26/2024
4979490PENDING (CUST) VIOLATION REPAIR - Assigned.

Assigned Role - Licensee.
9/5/2024 0:001119/5/2024
 
 
Anonymous
Not applicable

Hi,@willb006 ,thank you for your reply.
According to your latest information, you need to realize the effect of grouping calculation based on TicketID on the original basis, then you just need to change all the places in the code where ALL('Table') is used to ALLEXCEPT('Table'['ticket_number']), and let the original scope of the filter table which removes all the external filters be changed to just affected by the grouping of ['ticket_number'].
to achieve the effect of grouping (each ticket_number is counted internally).
like this:

vjtianmsft_0-1726019966922.png

result_column02 = 
VAR _date = [comment_sort]
VAR _numnotbalnk =
    CALCULATE (
        MAX ( 'Table'[comment_sort] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] < _date
                && NOT ( ISBLANK ( [wall_commentcreatedon] ) )
        )
    )
VAR diffday =
    CALCULATE (
        SUM ( 'Table'[Days] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] > _numnotbalnk
                && 'Table'[comment_sort] <= _date
        )
    )
VAR lastdate1 =
    CALCULATE (
        MAX ( 'Table'[wall_commentcreatedon] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] = _numnotbalnk
        )
    )
RETURN
    IF (
        ISBLANK ( 'Table'[wall_commentcreatedon] ),
        lastdate1 + diffday,
        'Table'[wall_commentcreatedon]
    )

Note that your data is quite specific and implements not really iterative logic (it looks like iteration, but it is not).
And fortunately, the source you gave has a comment_sort column to achieve an iteration-like effect (actually a conditional judgment based on sortnum, where each row is recalculated from the first row). If you don't provide a column for comment_sort, everything will get very tricky. (DAX itself doesn't support implementing iteration (it calculates uniformly based on a whole column, not on each cell, and there is no looping logic), and if the implementation wants true iteration logic, it's usually handled locally in the datasource or through Power Query using looping functions.
I hope my explanation can answer your questions.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Makes sense.  I was using th all except but only on the last step.  Now I am getting this error "A single value for column 'wall_commentcreatedon' in table 'Append1' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result." Append1 is my table name

Anonymous
Not applicable

Hi,@willb006 .Thank you for your reply.
Based on your description. I think the problem should be here:

vjtianmsft_1-1726104084019.png

 

Normally in the CALCULATE function, the expected value of the calculation should be an aggregated value (a single value, not just a column). This is because the CALCULATE function performs the calculation in the specified context, and if the returned result contains multiple values, DAX will not be able to determine what to do with them, resulting in an error.
The positions in the red area in the screenshot above should all be calculated using an aggregation function that returns a scalar value (i.e., a single value)

Location of the error code that I think might be wrong:

VAR lastdate1 = CALCULATE ( 'Table'[wall_commentcreatedon] , FILTER ( ALLEXCEPT ( 'Table', 'Table'[ticket_number] ), 'Table'[comment_sort] = _numnotbalnk ) )

change to this:

CALCULATE (
        MAX ( 'Table'[wall_commentcreatedon] ),// use MAX function
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[ticket_number] ),
            'Table'[comment_sort] = _numnotbalnk
        )
    )​

 


I have uploaded the complete pbix file, you can view the file and do a complete error check against the code above, I have not used any other meausre in the entire calculation and there are no other table relationships and the final results are displayed properly so I think you may have missed something in the code writing process (in the calculate function, the first parameter in the calculate function must be an aggregate value, not a column name with multiple values), good luck figuring out what's wrong.

I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I appreciate all you help with this v-jtian-msft  the issuse is in the last line of code 

VAR lastdate1 =
    CALCULATE (
        MAX ( 'Query1'[wall_commentcreatedon] ),
        FILTER (
            ALLEXCEPT ( 'Query1', 'Query1'[ticket_number] ),
            'Query1'[comment_sort] = _numnotblank
        )
    )
RETURN
    IF (
        ISBLANK ('Query1'[wall_commentcreatedon]),
        lastdate1 + diffday,
        'Query1'[wall_commentcreatedon]
    )
this is looking for an aggregate function.  I tried both MIN and MAX but that doesn't work because it's taking the MAX for the ticket number.  Would a filter with allexcept comment sort work since that is the unique value?  
Preview
 
 
 

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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