cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Working with nested if statements to change datasource

Hi all!

 

I am creating a section of the app where a user will click on a date in a datepicker, which will then display a DataTable with information relevant to that date. Each date has information found in a different excel table. This is the datasource of a DataTable which is saved as "TableName".

 

I have used the following code on 'OnSelect' on a button however it doesn't seem to be working, refreshes seem to do nothing. Can anyone guide me how to fix this?

 

If(
   OldDate=DatePicker1.SelectedDate, 
     UpdateContext({PopUpDate:false }),
     If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=1, 
         UpdateContext({TableName: Date_1}),
          If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=2, 
                    UpdateContext({TableName: Date_2}),
                         If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=3, 
                             UpdateContext({TableName: Date_3}),
                             If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=4, 
                                  UpdateContext({TableName: Date_4}),
                                    If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=5, 
                                             UpdateContext({TableName: Date_5}),
                                             If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=6, 
                                                   UpdateContext({TableName: Date_6}),
                                                   If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=7, 
                                                          UpdateContext({TableName: Date_7}),
                                                           If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=8, 
                                                                    UpdateContext({TableName: Date_8}),
                                                                     If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=9, 
                                                                              UpdateContext({TableName: Date_9}),
                                                                              If(DateDiff(DatePicker1.SelectedDate, Today(), Days)=10, 
                                                                                         UpdateContext({TableName: Date_10}))))))))))))

; UpdateContext({PopUpDate:false})

 

EDIT: Also there's got to be a better way to achieve this than the solution i've come up with - please guide me to something more conscice if you know of a way

1 ACCEPTED SOLUTION

Accepted Solutions
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Working with nested if statements to change datasource

If you want to bind 'TableName' to a data table, then you need to make it a collection, not a variable - instead of using

UpdateContext({TableName: Date_1})

You would use

ClearCollect(TableName, Date_1)

Which would make a copy of Date_1 in TableName.

 

Also notice that the If function can take several conditions in one single call, so you don't need to have this large number of nested If calls - your expression is roughly equivalent to the one below:

If(
    OldDate = DatePicker1.SelectedDate, 
        UpdateContext({ PopUpDate:false }),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 1, 
        ClearCollect(TableName, Date_1),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 2,
        ClearCollect(TableName, Date_2),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 3,
        ClearCollect(TableName, Date_3),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 4,
        ClearCollect(TableName, Date_4),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 5,
        ClearCollect(TableName, Date_5),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 6,
        ClearCollect(TableName, Date_6),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 7,
        ClearCollect(TableName, Date_7),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 8,
        ClearCollect(TableName, Date_8),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 9,
        ClearCollect(TableName, Date_9),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 10,
        ClearCollect(TableName, Date_10))
; UpdateContext({ PopUpDate:false })

Another change you can make - since most of the comparisons are done with the same value (DateDiff(DatePicker1.SelectedDate, Today(), Days)), then you can use the Switch function to make this expression simpler:

If(
    OldDate = DatePicker1.SelectedDate, 
    UpdateContext({ PopUpDate:false }),
    Switch(
        DateDiff(DatePicker1.SelectedDate, Today(), Days),
        1, ClearCollect(TableName, Date_1),
        2, ClearCollect(TableName, Date_2),
        3, ClearCollect(TableName, Date_3),
        4, ClearCollect(TableName, Date_4),
        5, ClearCollect(TableName, Date_5),
        6, ClearCollect(TableName, Date_6),
        7, ClearCollect(TableName, Date_7),
        8, ClearCollect(TableName, Date_8),
        9, ClearCollect(TableName, Date_9),
        10, ClearCollect(TableName, Date_10)))
; UpdateContext({ PopUpDate:false })

The attached app shows how these expressions can be used.

 

Hope this helps!

4 REPLIES 4
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Working with nested if statements to change datasource

If you want to bind 'TableName' to a data table, then you need to make it a collection, not a variable - instead of using

UpdateContext({TableName: Date_1})

You would use

ClearCollect(TableName, Date_1)

Which would make a copy of Date_1 in TableName.

 

Also notice that the If function can take several conditions in one single call, so you don't need to have this large number of nested If calls - your expression is roughly equivalent to the one below:

If(
    OldDate = DatePicker1.SelectedDate, 
        UpdateContext({ PopUpDate:false }),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 1, 
        ClearCollect(TableName, Date_1),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 2,
        ClearCollect(TableName, Date_2),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 3,
        ClearCollect(TableName, Date_3),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 4,
        ClearCollect(TableName, Date_4),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 5,
        ClearCollect(TableName, Date_5),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 6,
        ClearCollect(TableName, Date_6),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 7,
        ClearCollect(TableName, Date_7),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 8,
        ClearCollect(TableName, Date_8),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 9,
        ClearCollect(TableName, Date_9),
    DateDiff(DatePicker1.SelectedDate, Today(), Days) = 10,
        ClearCollect(TableName, Date_10))
; UpdateContext({ PopUpDate:false })

Another change you can make - since most of the comparisons are done with the same value (DateDiff(DatePicker1.SelectedDate, Today(), Days)), then you can use the Switch function to make this expression simpler:

If(
    OldDate = DatePicker1.SelectedDate, 
    UpdateContext({ PopUpDate:false }),
    Switch(
        DateDiff(DatePicker1.SelectedDate, Today(), Days),
        1, ClearCollect(TableName, Date_1),
        2, ClearCollect(TableName, Date_2),
        3, ClearCollect(TableName, Date_3),
        4, ClearCollect(TableName, Date_4),
        5, ClearCollect(TableName, Date_5),
        6, ClearCollect(TableName, Date_6),
        7, ClearCollect(TableName, Date_7),
        8, ClearCollect(TableName, Date_8),
        9, ClearCollect(TableName, Date_9),
        10, ClearCollect(TableName, Date_10)))
; UpdateContext({ PopUpDate:false })

The attached app shows how these expressions can be used.

 

Hope this helps!

Anonymous
Not applicable

Re: Working with nested if statements to change datasource

Wohoo, that's brilliant! Thanks for explaining that all so clearly with those solutions Smiley Happy. I learned a lot reading that!

 

I'm having a little struggle still on this side with all TableName always being an empty collection... which seems very odd. I've set it up exactly how you demonstrated using the switch function yet it is always empty. Do you know why this would be the case?

 

Also thanks for creating a demo app  - Unfortunately I'm not able to open it. I've tried extracting it to a zip and opening from the import section of the website however that doesn't seem to work. Sorry for such a basic question but how to I open that type of file?

 

Kindest regards,

Colleen

 

 

PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Working with nested if statements to change datasource

To open the attached app, first download it (it will be named ForumThread148984.msapp or something similar), then on the PowerApps Studio, select File -> Open -> Browse, and select the file that you downloaded - as shown in the capture below.

OpenMsappFile.gif

 

And what you you mean by "TableName always being an empty collection"? Notice that based on your logic, it will only receive a value if the date selected in the date picker is one of the last 10 days; if it's any other day (such as today, any day in the future, or any day more than 11 days in the past), then nothing will happen to TableName, and it will continue empty.

Anonymous
Not applicable

Re: Working with nested if statements to change datasource

Thank you so much @CarlosFigueira - I really appreciate your time helping and explaining this to me Smiley Happy.

 

My prior problem was a silly logic mistake in that I was setting the collections up for 10 days in the past instead of what i inteded, ten days in the future. This works perfectly so cheers!

Helpful resources

Announcements
firstImage

PowerApps Monthly Community Call!

Join us next Wednesday for our Demo Extravaganza, October 16, 2019 8am PDT.

firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 370 members 4,779 guests
Please welcome our newest community members: