Skip to content

Instantly share code, notes, and snippets.

@bjulius
Last active December 15, 2024 16:15
Show Gist options
  • Save bjulius/a939dcb6d824385dfb53b84812d4be8e to your computer and use it in GitHub Desktop.
Save bjulius/a939dcb6d824385dfb53b84812d4be8e to your computer and use it in GitHub Desktop.
Brian Julius Solution to Excel BI Challenge 470
//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
@bjulius
Copy link
Author

bjulius commented Jun 5, 2024

//Here is the solution written by GPT4O without the use of the custom GPT

let
// Load the data
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

// Convert the column "Number" to text to handle digit manipulation
ConvertToText = Table.TransformColumns(Source, {{"Number", each Text.From(_, "en-US"), type text}}),

// Define a function to generate all permutations of a list using an iterative approach
PermutationsIterative = (list as list) as list =>
let
    InsertAtAllPositions = (x as text, lst as list) as list =>
        List.Transform({0..List.Count(lst)}, (i) => List.InsertRange(lst, i, {x})),
    
    AccumulatePermutations = List.Accumulate(list, {{}}, (state, current) =>
        List.Combine(List.Transform(state, each InsertAtAllPositions(current, _)))
    ),
    
    result = List.Transform(AccumulatePermutations, each Text.Combine(_))
in
    result,

// Define a function to find the next greater number with the same digits
NextGreaterNumber = (inputNumber as text) as nullable text =>
let
    splitDigits = Text.ToList(inputNumber),
    permutedDigits = List.Sort(List.Distinct(PermutationsIterative(splitDigits))),
    currentIndex = List.PositionOf(permutedDigits, inputNumber),
    nextNumber = if currentIndex < List.Count(permutedDigits) - 1 then permutedDigits{currentIndex + 1} else null
in
    nextNumber,

// Add a new column with the next greater number
AddNextGreaterNumberColumn = Table.AddColumn(ConvertToText, "NextGreaterNumber", each NextGreaterNumber([Number])),

// Handle cases where there is no greater number
ReplaceNulls = Table.ReplaceValue(AddNextGreaterNumberColumn, null, "No such number", Replacer.ReplaceValue, {"NextGreaterNumber"}),

// Convert the "NextGreaterNumber" column back to numbers where applicable
ConvertBackToNumber = Table.TransformColumns(ReplaceNulls, {{"NextGreaterNumber", each try Number.FromText(_) otherwise _}})

in
ConvertBackToNumber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment