Microsoft Power Fx Common Expressions
Introduction
Power Fx is a low-code, Excel-like programming language for canvas apps in Microsoft Power Apps. This cheat sheet provides quick reference to commonly used formulas, syntax, and patterns to accelerate your development.
1. Basic Syntax
- Comments:
// Single line comment
/* Multi-line comment */
true, false
"Text", 123, 123.45
Set(variableName, value); // Global variable
UpdateContext({contextVariable: value}); // Context variable
- Arithmetic:
+,-,*,/,^ - Comparison:
=,<>,>,<,>=,<= - Logical:
And,Or,Not
2. Working with Data
Tables
- Create a table:
Table({Column1: Value1, Column2: Value2}, {Column1: Value3, Column2: Value4})
Filter(TableName, ColumnName = "Value")
Sort(TableName, ColumnName, Ascending)
AddColumns(TableName, "NewColumn", Formula)
DropColumns(TableName, "ColumnName")
RenameColumns(TableName, "OldName", "NewName")
Records
- Create a record:
{Field1: Value1, Field2: Value2}
RecordName.FieldName
Patch(TableName, RecordToUpdate, {FieldName: NewValue})
3. Functions
Text Functions
- Concatenate:
Concatenate("Hello", " ", "World")
Upper("text")
Lower("TEXT")
Trim(" text ")
Mid("Text", StartIndex, Length)
Math Functions
- Rounding:
Round(Number, DecimalPlaces)
Rand()
Abs(Number)
Logical Functions
- Conditional:
If(Condition, TrueResult, FalseResult)
Switch(Expression, Value1, Result1, Value2, Result2, DefaultResult)
Date and Time Functions
- Current date/time:
Now()
Today()
DateDiff(StartDate, EndDate, Unit)
DateAdd(Date, Number, Unit)
4. User Interface Functions
- Navigate between screens:
Navigate(ScreenName, Transition)
Notify("Message", NotificationType)
Refresh(DataSource)
!BooleanVariable
5. Common Expressions
- Set Variables:
Set(UserName, "John Doe");
Set(UserAge, 30);
Navigate(HomeScreen, ScreenTransition.Fade);
UpdateContext({IsVisible: true});
If(IsLoggedIn, "Welcome back!", "Please sign in.");
Filter(Products, Category = "Electronics");
Sort(Employees, Name, Ascending);
SubmitForm(EditForm);
6. Complex Expressions
- Chained Filtering and Sorting:
Sort(Filter(Products, Category = "Electronics" And Price > 100), Price, Descending)
Filter(Products, (Category = Dropdown.Selected.Value Or IsBlank(Dropdown.Selected.Value)) And (StartsWith(Name, SearchBox.Text) Or IsBlank(SearchBox.Text)))
Filter(Subcategories, CategoryID = DropdownCategories.Selected.ID)
Sum(Filter(Sales, Region = Dropdown.Selected.Value), Amount)
ForAll(Sequence(CountRows(Products)), {Index: Value})
If(ThisItem.Stock < 10, Color.Red, Color.Green)
If(IsEmpty(Filter(Products, ID = InputID.Text)), Notify("Product not found!", NotificationType.Error), Navigate(ProductDetailsScreen))
For more detailed documentation, visit the official Microsoft Power Fx documentation.
No comments:
Post a Comment