cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sasrsc
Resolver II
Resolver II

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

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)))))

})

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User III
Super User III

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

2 REPLIES 2
RandyHayes
Super User III
Super User III

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

View solution in original post

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)
)
})

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (3,651)