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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

CONDITIONAL DATES

Hi,

 

I'm developing a daily report in Power BI, but if the user select a date where there´s no data, Power BI should select data from the last date with information:

 

CASE 1 - The user selected a date with data: Power BI should list the information for the selected date.

CASE 2 - The user selected a date with no data: Power BI should show information from the last date for each Facility:

 

Facilities    Date              Values

---------------------------------

FAC001     2021-08-16   3,000

FAC002     2020-10-01   2,500

FAC003     2019-11-03   4,000

 

Is there a way to do this?

 

Thank you,

 

Benedito Almeida.

1 ACCEPTED SOLUTION

Try:

Sum Values =
VAR MaxDate = MAXX(FILTER(ALL(Table[Date), Table[Date] <= SELECTEDVALUE(IndDate[Date]) && NOT(ISBLANK(SUM(Table[Values])))), Table[Date])

RETURN

CALCULATE(SUM(Table[Values]), FILTER(ALL(Table[Date]), Table[Date] = MaxDate)




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

8 REPLIES 8
v-zhangti
Community Support
Community Support

Hi, @Anonymous 

 

I simulated some optional dates, hoping to achieve your expected results.

  1. Create a new column in the Date table.

 

New date = 
IF (
    [Selectable date]
        = CALCULATE (
            MAX ( 'Table'[Date ] ),
            FILTER ( 'Table', [Date ] = EARLIER ( 'Date'[Selectable date] ) )
        ),
    [Selectable date],
    CALCULATE (
        MAX ( 'Table'[Date ] ),
        FILTER ( 'Table', [Date ] < EARLIER ( 'Date'[Selectable date] ) )
    )
)
New value = 
CALCULATE (
    MAX ( 'Table'[Values] ),
    FILTER ( 'Table', [Date] = EARLIER ( 'Date'[New date] ) )
)

 

vzhangti_0-1637034399495.png

 

    2. The new Date table needs to establish a relationship with the original table.

vzhangti_1-1637034438085.png

 

  3. Create a new date slicer in the view, and the final result is shown in the figure.

vzhangti_2-1637034464280.jpeg

 

If the method I provided above can't solve your problem, what's your expected result? Could you please provide more details for it?

 

Best Regards,

Community Support Team _Charlotte

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

 

 

 

Anonymous
Not applicable

Hello, everyone

I would like to thank you all for your help.

I've decided to create a stored procedure to select the data I need for the report.

The SP is working corretly.

My question now is: can I pass the report slicer (it's a date) as a parameter to the stored procedure?

This would solve my problem completely.

Thank you all.

Benedito_1967.

Hi, @Anonymous 

 

Create a new calendar table.

 

Date = CALENDAR(MIN('Table'[Date]),MAX('Table'[Date]))

 

 

New measure takes the date selected in the slicer.

 

New Date = MAX('Date'[Date])
Output date = 
IF (
    [New Date] = MAX ( 'Table'[Date] ),
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( ALL ( 'Table' ), [Date] = MAX ( 'Table'[Date] ) )
    ),
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( ALL ( 'Table' ), [Date] < MAX ( 'Date'[Date] ) )
    )
)
New Facilities =
CALCULATE (
    MAX ( 'Table'[Facilities] ),
    FILTER ( ALL ( 'Table' ), [Output date] = MAX ( 'Table'[Date] ) )
)
New value = 
CALCULATE (
    MAX ( 'Table'[Values] ),
    FILTER ( ALL('Table'), [Date] =MAX('Table'[Date] ) )
)

 

vzhangti_0-1637650637877.png

 

Is this the case when the date slicer you mentioned is selected as a parameter? If you have any other questions, please reply.

 

Best Regards,

Community Support Team _Charlotte

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

Anonymous
Not applicable

Hello,

I would like to thank you all for your help.

You really saved me hours of work.

Benedito_1967.

PaulDBrown
Community Champion
Community Champion

You will need an independent date table for the slicer. Let's call this table IndDate

Sum Values =
VAR MaxDate = MAXX(FILTER(ALL(Table), Table [Date] <= SELECTEDVALUE(IndDate[Date]) && NOT(ISBLANK(SUM(Table[Values])))), Table[Date])

RETURN

CALCULATE(SUM(Table[Values]), FILTER(ALL(Table), Table[Date] = MaxDate)





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Good morning, Paul

Thank you very much for your input.

I tried to use your sugestion, but the report now returns the same value for all FACILITIES, like this:

Facilities    Date              Values

---------------------------------

FAC001     2021-08-16   90.24

FAC002     2021-08-16   90.24

FAC003     2021-08-16   90.24

What I really need is that for each FACILITY the report returns its last date and value.

Benedito Almeida.

Try:

Sum Values =
VAR MaxDate = MAXX(FILTER(ALL(Table[Date), Table[Date] <= SELECTEDVALUE(IndDate[Date]) && NOT(ISBLANK(SUM(Table[Values])))), Table[Date])

RETURN

CALCULATE(SUM(Table[Values]), FILTER(ALL(Table[Date]), Table[Date] = MaxDate)




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Greg_Deckler
Super User
Super User

@Anonymous Maybe something like:

Measure =
  VAR __Date = SELECTEDVALUE('Calendar'[Date])
  VAR __Value = CALCULATE(SUM('Table'[Values]),[Date]=__Date)
  VAR __NewDate = IF(ISBLANK(__Value),MAX('Table'[Date]),__Date)
RETURN
  CALCULATE(SUM('Table'[Values]),[Date]=__NewDate)

@ 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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.