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
Anonymous
Not applicable

Last value for each customer

Hi,

 

I have a problem with a measure that should return the last value for a given customer. Sample excel table:

DateCustomerMeasureBrand
16.09.2021A100x
16.09.2021B110x
16.09.2021C120x
16.09.2021D130x
16.09.2021D5y
16.09.2021E140x
17.09.2021A150x
17.09.2021B160x
17.09.2021C170x
17.09.2021E180x
18.09.2021A190x
18.09.2021B200x
24.09.2021C210x

 

I wrote the measure below:

SUMX (
VALUES ( Arkusz1[Customer] ),
CALCULATE ( SUM(Arkusz1[Measure] ), LASTDATE ( Arkusz1[Date] ) )
)
 
It works fine until I turn on aggregation after e.g. weeks. The problem is that if in a given week / other period there is no value for a given customer, I would like it to take from the previous period (so far it returns 0). Is it possible for the measure to be context-sensitive on the one hand and for the blank to take the last non-empty value on the other hand? Now I have:
screen.png
To sum up: I want the measure to be actually calculated in the context of the week as it is now, but in the case of a blank it should take the last available value for each customer.
 
Best Regards!
1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @Anonymous ,

 

Here's my solution.

1.Create a calendar table and there's no relationship between two tables.

Calendar = ADDCOLUMNS(CALENDARAUTO(),"Weeknum",WEEKNUM([Date],2))

4.png

 

2.Create a Weeknum column in Arkusz1 table.

Weeknum = WEEKNUM([Date],2)

vstephenmsft_0-1632727626813.png

 

3.Measure 3 is the measure you created.

Measure 2 = SUMX (
VALUES('Arkusz1'[Customer]),
CALCULATE ( SUM('Arkusz1'[Measure]), LASTDATE ( 'Arkusz1'[Date] ) )
)

 

4.Create the following measure

LastDateByWeeknumWithoutBlank = 
var _value=CALCULATE([Measure 2],FILTER('Arkusz1',[Weeknum]=MAX('Calendar'[Weeknum])))
var _valuelastweek=CALCULATE([Measure 2],FILTER('Arkusz1',[Weeknum]=MAX('Calendar'[Weeknum])-1))
return
IF(_value=BLANK(),_valuelastweek,_value)

5.png

 

5.If you only want to limit the number of weeks to only the number of weeks in your main table.

   Create a flag measure and put it into Filters.

flag = var _max=MAXX(ALL(Arkusz1),[Weeknum])
var _min=MINX(ALL(Arkusz1),[Weeknum])
return IF(_min<=MAX('Calendar'[Weeknum])&&_max>=MAX('Calendar'[Weeknum]),1)

6.png

 

 

Best Regards,

Stephen Tao

 

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

5 REPLIES 5
v-stephen-msft
Community Support
Community Support

Hi @Anonymous ,

 

Here's my solution.

1.Create a calendar table and there's no relationship between two tables.

Calendar = ADDCOLUMNS(CALENDARAUTO(),"Weeknum",WEEKNUM([Date],2))

4.png

 

2.Create a Weeknum column in Arkusz1 table.

Weeknum = WEEKNUM([Date],2)

vstephenmsft_0-1632727626813.png

 

3.Measure 3 is the measure you created.

Measure 2 = SUMX (
VALUES('Arkusz1'[Customer]),
CALCULATE ( SUM('Arkusz1'[Measure]), LASTDATE ( 'Arkusz1'[Date] ) )
)

 

4.Create the following measure

LastDateByWeeknumWithoutBlank = 
var _value=CALCULATE([Measure 2],FILTER('Arkusz1',[Weeknum]=MAX('Calendar'[Weeknum])))
var _valuelastweek=CALCULATE([Measure 2],FILTER('Arkusz1',[Weeknum]=MAX('Calendar'[Weeknum])-1))
return
IF(_value=BLANK(),_valuelastweek,_value)

5.png

 

5.If you only want to limit the number of weeks to only the number of weeks in your main table.

   Create a flag measure and put it into Filters.

flag = var _max=MAXX(ALL(Arkusz1),[Weeknum])
var _min=MINX(ALL(Arkusz1),[Weeknum])
return IF(_min<=MAX('Calendar'[Weeknum])&&_max>=MAX('Calendar'[Weeknum]),1)

6.png

 

 

Best Regards,

Stephen Tao

 

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

 

Anonymous
Not applicable

Maybe I will add that the dates on the visualization come from a different date table (calendarauto)

Greg_Deckler
Super User
Super User

@Anonymous Maybe:

New Measure = 
  VAR __Customer = MAX('Table'[Customer])
  VAR __Date = MAX('Table'[Date])
  VAR __Value1 = SUMX ( VALUES ( Arkusz1[Customer] ), CALCULATE ( SUM(Arkusz1[Measure] ), LASTDATE ( Arkusz1[Date] ) )
  VAR __MaxDate = MAXX(FILTER(ALL('Table'),[Customer]=__Customer && [Date]<__Date),[Date])
  VAR __Value2 = SUMX ( VALUES ( Arkusz1[Customer] ), CALCULATE ( SUM(Arkusz1[Measure] ), 'Table'[Date] = __MaxDate ) )
)
RETURN
  IF(ISBLANK(__Value1),__Value2,__Value1)

Something along those lines. 


@ 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...
Anonymous
Not applicable

Unfortunately, it doesn't work for me:(

Anonymous
Not applicable

Spoiler
Nobody?

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.