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 / Working with nested if...
Power Apps
Answered

Working with nested if statements to change datasource

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

Categories:
  • Verified answer
    CarlosFigueira Profile Picture
    Microsoft Employee on at

    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!

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Wohoo, that's brilliant! Thanks for explaining that all so clearly with those solutions :). 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

     

     

  • CarlosFigueira Profile Picture
    Microsoft Employee on at

    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.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

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

     

    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!

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

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 220 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 194 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 150

Last 30 days Overall leaderboard