cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
akfir
Helper IV
Helper IV

Calculated column of previous value by date & customer

akfir_0-1646920667536.png

Hi guys,
i wish to add a calculated column as in E column above which calculates the previous grade (by date and customer). please note that in my table i have rows which do not hold a grade and presenting blank value.

3 ACCEPTED SOLUTIONS
tamerj1
Community Champion
Community Champion

We may reduce the number of columns if you have too many. For now please try this

Previous Grade =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Customer Code] ) )
VAR T2 =
    FILTER ( 'T1', [Date] < CurrentDate )
VAR T3 =
    TOPN ( 1, 'T2', [Date] )
VAR T4 =
    SELECTCOLUMNS ( 'T3', "@PGrade", [Grade] )
RETURN
    Result

View solution in original post

tamerj1
Community Champion
Community Champion

 

Previous Date =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Customer Code] ) )
VAR T2 =
    FILTER ( 'T1', [Date] < CurrentDate )
VAR T3 =
    TOPN ( 1, 'T2', [Date] )
VAR Result =
    SELECTCOLUMNS ( 'T3', "@Date", [Date] )
RETURN
    Result

 

View solution in original post

tamerj1
Community Champion
Community Champion

Try this

First Purchase Channel =
CALCULATE (
    VALUES ( Data[Purchase Channel] ),
    ALLEXCEPT ( Data, Data[Customer Code] ),
    Data[Date] = Data[First Purchase Date]
)

View solution in original post

26 REPLIES 26
tamerj1
Community Champion
Community Champion

Hi @akfir 

you can try
 

 

Previous Grade =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE (
        'Table',
        ALLEXCEPT ( 'Table', 'Table'[Customer Code] ),
        'Table'[Date] < CurrentDate
    )
VAR T2 =
    TOPN ( 1, 'T1', [Date] )
VAR T3 =
    SELECTCOLUMNS ( 'T2', "@PGrade", [Grade] )
RETURN
    Result

 

there is no RESULT variable to return....

tamerj1
Community Champion
Community Champion

Previous Grade =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE (
        'Table',
        ALLEXCEPT ( 'Table', 'Table'[Customer Code] ),
        'Table'[Date] < CurrentDate
    )
VAR T2 =
    TOPN ( 1, 'T1', [Date] )
VAR Result =
    SELECTCOLUMNS ( 'T2', "@PGrade", [Grade] )
RETURN
    Result

<pi>there's not enough memory to complete this operation. please try again later when there may be more memory available.</pi>

tamerj1
Community Champion
Community Champion

How big is your table?

30K rows

tamerj1
Community Champion
Community Champion

We may reduce the number of columns if you have too many. For now please try this

Previous Grade =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Customer Code] ) )
VAR T2 =
    FILTER ( 'T1', [Date] < CurrentDate )
VAR T3 =
    TOPN ( 1, 'T2', [Date] )
VAR T4 =
    SELECTCOLUMNS ( 'T3', "@PGrade", [Grade] )
RETURN
    Result

akfir_0-1647182164569.png

Why is that? 

this is something related to T4, because the previous date column that you helped me as well - still valid and not showing any error.

tamerj1
Community Champion
Community Champion

@akfir 

That means you have multiple records in the same date. Then you can only aggregate by sum or max/min or otherwise provide more filters that can restrict the result to only one record. 

thats maybe because i transferred DATE from date/time to date. what can i do?

tamerj1
Community Champion
Community Champion

You have to restore the date time format in order maitain accuracy 

Perfect thanks! now i wish to add a column of the date referring to this previous value - in my example above - for the first row i want the value to be 01/02/2022

tamerj1
Community Champion
Community Champion

 

Previous Date =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Customer Code] ) )
VAR T2 =
    FILTER ( 'T1', [Date] < CurrentDate )
VAR T3 =
    TOPN ( 1, 'T2', [Date] )
VAR Result =
    SELECTCOLUMNS ( 'T3', "@Date", [Date] )
RETURN
    Result

 

Awsome!! thanks!

tamerj1
Community Champion
Community Champion

@akfir 
If my reply solved your problem please consider marking it as an acceptable solution. Thank you and have a great day!

tamerj1
Community Champion
Community Champion

How many columns?

tamerj1
Community Champion
Community Champion

@akfir 

You may also try this

Previous Grade =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Customer Code] ) )
VAR T2 =
    ADDCOLUMNS ( 'T1', "@Ranking", RANKX ( 'T1', [Date] ) )
VAR T3 =
    ADDCOLUMNS (
        'T2',
        "@PGrade",
            VAR CurrentRank = [@Ranking]
            RETURN
                LOOKUPVALUE ( [Grade], [@Ranking], CurrentRank + 1 )
    )
VAR T4 =
    FILTER ( 'T3', [Date] = CurrentDate )
VAR Result =
    SUMX ( 'T4', [@PGrade] )
RETURN
    Result

Hi my friend,
can you please help me build a column of the first purchase date by customer? as attached...image.png

tamerj1
Community Champion
Community Champion

Hi @akfir 

First Purchase Date =
CALCULATE (
    MIN ( 'Table'[Date] ),
    ALLEXCEPT ( 'Table', 'Table'[Customer Code] )
)

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors