├── Number.Mod ├── Number.ModX.pq └── README.md ├── README.md └── Table.Sort ├── README.md └── Table.Sort.pq /Number.Mod/Number.ModX.pq: -------------------------------------------------------------------------------- 1 | let func = 2 | 3 | (number, divisor) => if number>0 then number-Number.RoundDown(number/divisor,0) else Number.Abs(divisor)+number 4 | 5 | , documentation = [ 6 | Documentation.Name = " fnNumber.Mod 7 | ", Documentation.Description = " simple Number.Mod that returns the same like Excel or DAX when negative figures are involved 8 | " , Documentation.LongDescription = " simple Number.Mod that returns the same like Excel or DAX when negative figures are involved 9 | ", Documentation.Category = " Number.Operations 10 | ", Documentation.Source = " https://github.com/ImkeF/Caveats-in-native-M-functions/tree/master/Number.Mod . 11 | ", Documentation.Author = " Imke Feldmann: www.TheBIccountant.com 12 | ", Documentation.Examples = {[Description = " (number, divisor) 13 | " , Code = " 14 | ", Result = " 15 | "]}] 16 | in 17 | Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation)) 18 | -------------------------------------------------------------------------------- /Number.Mod/README.md: -------------------------------------------------------------------------------- 1 | # Number.Mod 2 | 3 | ## About 4 | 5 | Divides two numbers and returns the remainder of the resulting number. 6 | 7 | ```Number.Mod(number as nullable number, divisor as nullable number, optional precision as nullable number) as nullable number ``` 8 | 9 | 10 | ## Arguments 11 | 12 | | Argument | Description | 13 | | ------------------------- | --------------------------------- | 14 | | number | Dividend or numerator | 15 | | divisor | Divisor or denominator | 16 | 17 | ## Example 18 | 19 | ```Number.Mod(83, 9) ``` equals 2 20 | 21 | 22 | 23 | ## Caveats 24 | - Diverging signs at numerator and divisor will lead to different results compared to Excel and DAX (https://www.thebiccountant.com/2017/09/13/number-mod-rescue-pack-power-bi-power-query/) 25 | 26 | 27 | ## Alternatives 28 | - [Number.ModX](https://github.com/ImkeF/M/blob/master/Library/Number.ModXls.pq) : Returns the same results than the modulo-function in Excel and DAX 29 | 30 | 31 | ## Performance considerations 32 | 33 | ## Useful links 34 | 35 | ## Link to official documentation 36 | https://msdn.microsoft.com/en-us/library/mt253344.aspx 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M-Guide 2 | 3 | This repository holds alternative function documentation for the M-language, including the following categories: 4 | 5 | - Arguments 6 | - Remarks 7 | - Examples 8 | 9 | - **Caveats** 10 | 11 | - **Effect on query folding** 12 | 13 | - **Alternatives** 14 | 15 | - **Performance considerations** 16 | 17 | - **Useful links** 18 | 19 | - Link to official documentation 20 | 21 | Pull requests are very welcome. 22 | -------------------------------------------------------------------------------- /Table.Sort/README.md: -------------------------------------------------------------------------------- 1 | # Table.Sort 2 | 3 | Sorts the rows in a table using a comparisonCriteria or a default ordering if one is not specified. 4 | 5 | ```Table.Sort(table as table, optional comparisonCriteria as any) as table ``` 6 | 7 | ## Arguments 8 | 9 | 10 | Argument | Description 11 | -------- | ----------- 12 | table | The Table to modify. 13 | optional comparisonCriteria | Sort comparison criteria. 14 | 15 | ## Remarks 16 | 17 | Table.Sort is similar to List.Sort but requires a table as input. 18 | 19 | ## Examples 20 | 21 | ```Table.Sort( 22 | Table.FromRecords( 23 | { 24 | l 25 | [OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100.0], 26 | 27 | [OrderID = 2, CustomerID = 1, Item = "1 lb. worms", Price = 5.0], 28 | 29 | [OrderID = 3, CustomerID = 2, Item = "Fishing net", Price = 25.0], 30 | 31 | [OrderID = 4, CustomerID = 3, Item = "Fish tazer", Price = 200.0], 32 | 33 | [OrderID = 5, CustomerID = 3, Item = "Bandaids", Price = 2.0], 34 | 35 | [OrderID = 6, CustomerID = 1, Item = "Tackle box", Price = 20.0], 36 | 37 | [OrderID = 7, CustomerID = 5, Item = "Bait", Price = 3.25], 38 | 39 | [OrderID = 8, CustomerID = 5, Item = "Fishing Rod", Price = 100.0], 40 | 41 | [OrderID = 9, CustomerID = 6, Item = "Bait", Price = 3.25] 42 | 43 | } 44 | 45 | ), 46 | 47 | {{"CustomerID", Order.Ascending}, "OrderID"}) ``` 48 | ```` 49 | 50 | OrderID | CustomerID | Item | Price 51 | ------- | ---------- | ---- | ----- 52 | 1 | 1 | Fishing rod | 100 53 | 2 | 1 | 1 lb. worms | 5 54 | 6 | 1 | Tackle box | 20 55 | 3 | 2 | Fishing net | 25 56 | 4 | 3 | Fish tazer | 200 57 | 5 | 3 | Bandaids | 2 58 | 7 | 5 | Bait | 3.25 59 | 8 | 5 | Fishing Rod | 100 60 | 9 | 6 | Bait | 3.25 61 | 62 | 63 | ## Caveats 64 | - The sort order will not always be kept within further steps of the query. This is by design, see comment from Tristan St-Cyr: https://social.technet.microsoft.com/Forums/WINDOWS/en-US/30e9ab27-23a5-465b-a0c4-36e4e48ce2db/bug-or-feature-when-querying-sqlserver-data?forum=powerquery. 65 | - To make the sort order stick, you have to wrap the function expression into a Table.Buffer like so: Table.Buffer(Table.Sort(...YourSortExpression...)), or see this blogpost: https://community.powerbi.com/t5/Community-Blog/Bug-warning-for-Table-Sort-and-removing-duplicates-in-Power/ba-p/810390 66 | 67 | 68 | ## Alternatives 69 | https://github.com/ImkeF/M/blob/master/Library/Table.SortB.pq 70 | 71 | ## Performance considerations 72 | -- 73 | ## Useful links 74 | -- 75 | ## Link to official documentation 76 | https://msdn.microsoft.com/en-us/library/mt260813.aspx 77 | -------------------------------------------------------------------------------- /Table.Sort/Table.Sort.pq: -------------------------------------------------------------------------------- 1 | let func = 2 | (table as table, optional comparisonCriteria as any) => 3 | 4 | Table.Buffer(Table.Sort(table, comparisonCriteria)) 5 | , documentation = [ 6 | Documentation.Name = " Table.Sort.pq 7 | ", Documentation.Description = " Buffered Table.Sort 8 | " , Documentation.LongDescription = " Buffered Table.Sort. Buffers the result to maintain the sort order in future referencing steps 9 | ", Documentation.Category = " Table Transformation 10 | ", Documentation.Source = " local 11 | ", Documentation.Author = " Imke Feldmann: www.TheBIccountant.com . 12 | ", Documentation.Examples = {[Description = " 13 | " , Code = " 14 | ", Result = " 15 | "]}] 16 | in 17 | Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation)) 18 | 19 | --------------------------------------------------------------------------------