web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Update Context conditi...
Power Apps
Answered

Update Context conditionally set variable to a date picker...

(0) ShareShare
ReportReport
Posted on by 678

For some reason I can't paste the image so you can see the dropdown with the datepicker objects (for a start and end value)

DateDropdown is a dropdown containing...
["All Items","This Month", "Last Month", "This Year", "Last Year", "x Days Ago", "Custom"]

I have handled all possibilities except the custom one ...
I am setting the value of Start2 and End2 based on the value selected.

 

 


UpdateContext({
Start2:
If(DateDropdown.Selected.Value="This Month",Date(Year(Now()),Month(Now()),1),
If(DateDropdown.Selected.Value="Last Month",Date(Year(Now()),Month(Now())-1,1),
If(DateDropdown.Selected.Value="Last Year",Date(Year(Now())-1,1,1),
If(DateDropdown.Selected.Value="x Days Ago",DateAdd(Today(),-Slider1.Value),
Date(Year(Now()),1,1))))),
End2:
If(DateDropdown.Selected.Value="This Month",Date(Year(Now()),Month(Now())+1,0),
If(DateDropdown.Selected.Value="Last Month",Date(Year(Now()),Month(Now()),0),
If(DateDropdown.Selected.Value="Last Year",Date(Year(Now())-1,12,31),
If(DateDropdown.Selected.Value="x Days Ago",Today(),
Date(Year(Now()),12,31)))))

})

So, I want to add ...

If(DateDropdown.Selected.Value="Custom",DateFrom.SelectedDate), and I add an extra ) to the end of the clause and now it barks at me... saying Invalid argument type (date)


UpdateContext({
Start2:
If(DateDropdown.Selected.Value="This Month",Date(Year(Now()),Month(Now()),1),
If(DateDropdown.Selected.Value="Last Month",Date(Year(Now()),Month(Now())-1,1),
If(DateDropdown.Selected.Value="Last Year",Date(Year(Now())-1,1,1),
If(DateDropdown.Selected.Value="x Days Ago",DateAdd(Today(),-Slider1.Value),
If(DateDropdown.Selected.Value="Custom",DateFrom.SelectedDate),
Date(Year(Now()),1,1)))))),
End2:
If(DateDropdown.Selected.Value="This Month",Date(Year(Now()),Month(Now())+1,0),
If(DateDropdown.Selected.Value="Last Month",Date(Year(Now()),Month(Now()),0),
If(DateDropdown.Selected.Value="Last Year",Date(Year(Now())-1,12,31),
If(DateDropdown.Selected.Value="x Days Ago",Today(),
Date(Year(Now()),12,31)))))

})

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @sasrsc 

    Because you are compounding If statements, you are missing ending parenthesis.

    However, to avoid that confusion, you might want to consider the following formula instead:

    UpdateContext(
     {
     Start2:
     Switch(DateDropdown.Selected.Value,
     "This Month", Date(Year(Now()), Month(Now()), 1),
     "Last Month", Date(Year(Now()), Month(Now())-1, 1),
     "Last Year", Date(Year(Now())-1, 1, 1),
     "x Days Ago", DateAdd(Today(), -Slider1.Value), 
     Date(Year(Now()), 1, 1)
     ),
     End2:
     Switch(DateDropdown.Selected.Value,
     "This Month", Date(Year(Now()), Month(Now())+1, 0),
     "Last Month", Date(Year(Now()), Month(Now()), 0),
     "Last Year", Date(Year(Now())-1, 12, 31),
     "x Days Ago", Today(),
     Date(Year(Now()),12,31)
     )
     }
    )

    A little easier to read and to change.

     

    I hope this is helpful for you.

  • sasrsc Profile Picture
    678 on at

    That's so much cleaner...very eloquent - thank you

    In case anyone was wondering 😉

    Including the datepickers (I didn't need those extra () for that...

    UpdateContext({
    Start2:
    Switch(DateDropdown.Selected.Value,
    "This Month",Date(Year(Now()),Month(Now()),1),
    "Last Month",Date(Year(Now()),Month(Now())-1,1),
    "Last Year",Date(Year(Now())-1,1,1),
    "x Days Ago",DateAdd(Today(),-Slider1.Value),
    "Custom",DateFrom.SelectedDate,
    Date(Year(Now()),1,1)
    ),
    End2:
    Switch(DateDropdown.Selected.Value,
    "This Month",Date(Year(Now()),Month(Now())+1,0),
    "Last Month",Date(Year(Now()),Month(Now()),0),
    "Last Year",Date(Year(Now())-1,12,31),
    "x Days Ago",Today(),
    "Custom",DateTo.SelectedDate,
    Date(Year(Now()),12,31)
    )
    })

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 392 Most Valuable Professional

#2
11manish Profile Picture

11manish 181 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 112 Super User 2026 Season 2

Last 30 days Overall leaderboard