Last active
December 15, 2024 16:15
-
-
Save bjulius/a939dcb6d824385dfb53b84812d4be8e to your computer and use it in GitHub Desktop.
Brian Julius Solution to Excel BI Challenge 470
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is the solution written by GPT4O using my "Power BI Godzilla" custom GPT | |
let | |
// Load the table named Table1 | |
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], | |
// Define the custom function to find the next greater number | |
NextGreaterNumber = (input as number) as any => | |
let | |
digits = Text.ToList(Text.From(input)), | |
len = List.Count(digits), | |
FindSwapIndex = List.Transform({0..len-2}, each [Index=_, Digit=Number.FromText(digits{_})]), | |
ValidIndices = List.Select(FindSwapIndex, each _[Digit] < Number.FromText(digits{_[Index] + 1})), | |
swapIndex = if List.Count(ValidIndices) = 0 then null else List.Max(List.Transform(ValidIndices, each _[Index])), | |
newDigits = | |
if swapIndex = null then | |
"No such number" | |
else | |
let | |
rightPart = List.Skip(digits, swapIndex + 1), | |
leftPart = List.FirstN(digits, swapIndex), | |
swapWith = List.Min(List.Select(rightPart, each Number.FromText(_) > Number.FromText(digits{swapIndex}))), | |
newRightPart = List.Sort(List.RemoveItems(rightPart, {swapWith}) & {digits{swapIndex}}), | |
result = Text.Combine(leftPart & {swapWith} & newRightPart) | |
in | |
result | |
in | |
newDigits, | |
// Add a column with the next greater number for each row in the table | |
AddNextGreaterNumber = Table.AddColumn(Source, "NextGreaterNumber", each NextGreaterNumber([Number]), type text) | |
in | |
// Return the modified table | |
AddNextGreaterNumber |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//Here is the solution written by GPT4O without the use of the custom GPT
let
// Load the data
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
in
ConvertBackToNumber