Skip to content

Instantly share code, notes, and snippets.

@bjulius
bjulius / gist:e80cabe985a714bf608b7dba116e26e1
Created March 13, 2024 07:17
Excel BI Challenge 411 - Brian Julius solution
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
Dupe = Table.DuplicateColumn(AddIndex, "Sentences", "Sentences2"),
SplitToRows1 = Table.ExpandListColumn(Table.TransformColumns(Dupe, {{"Sentences2", Splitter.SplitTextByDelimiter(" """, QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Sentences2"),
SplitToRows2 = Table.ExpandListColumn(Table.TransformColumns(SplitToRows1, {{"Sentences2", Splitter.SplitTextByDelimiter(""" ", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Sentences2"),
IdentifyAddlSplits = Table.AddColumn(SplitToRows2, "Custom", each [
a = [Sentences],
b = Text.ToList( a ),
c = List.Select( b, each _ = """" ),
@bjulius
bjulius / gist:fffa738cd5e914a2bac88b56f28efc68
Last active March 4, 2024 02:34
Brian Julius Solution to CH-15 Transnformation
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
AddAttrib = Table.AddColumn(Source, "Attrib", each [
a = {"A".."Z"},
b = List.PositionOf( a, [Product Code]) + 1
][b]),
Group = Table.Group(AddAttrib, {"Product Code"}, {{"All", each _, type table [Product Code=text, Ship Date=datetime, Po number=number, Po Quantity=number, Attrib=number]}}),
AddWGIndex = Table.RemoveColumns( Table.AddColumn(Group, "WithinGroupIndex", each Table.AddIndexColumn( [All], "WGIndex", 1, 1)), "All"),
Expand = Table.ExpandTableColumn(AddWGIndex, "WithinGroupIndex", {"Ship Date", "Po number", "Po Quantity", "Attrib", "WGIndex"}, {"Ship Date", "Po number", "Po Quantity", "Attrib", "WGIndex"}),
UnpivotOther = Table.UnpivotOtherColumns(Expand, {"Attrib", "WGIndex"}, "Attribute.1", "Value"),
@bjulius
bjulius / gist:9940423079e266bf40914ccacf77499f
Created February 29, 2024 00:52
List. Accumulate vs List Transform
let
Source = {1..100000},
Accumulate =
List.Accumulate(
Source,
"",
( state, current ) => state & ( if state = "" then "" else ", " ) & Text.From( current )
),
SplitToList = List.Buffer( Text.Split(Accumulate, """, """)),
@bjulius
bjulius / gist:d39d1922a7c1b5a12cc77308856b927f
Created February 17, 2024 06:41
Excel BI Power Query Challenge 157 - Brian Julius Solution
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
AddGlobalOrigOrder = Table.AddIndexColumn(Source, "GlobalOrigOrder", 0, 1, Int64.Type),
UnpivotOther = Table.UnpivotOtherColumns(AddGlobalOrigOrder, {"Group", "GlobalOrigOrder"}, "Attribute", "Value"),
SortNBuffer = Table.Buffer( Table.Sort(UnpivotOther,{{"Group", Order.Ascending}, {"Attribute", Order.Ascending}, {"GlobalOrigOrder", Order.Ascending}})),
Group = Table.Group(SortNBuffer, {"Group", "Attribute"}, {{"All", each _, type table [Group=number, GlobalOrigOrder=number, Attribute=text, Value=any]}}),
AddGlobalIdx1 = Table.AddColumn(Group, "GlobalIdx1", each Table.AddIndexColumn([All], "Global1", 1, 1)),
AddGlobalIdx0 = Table.SelectColumns( Table.AddColumn(AddGlobalIdx1, "GlobalIdx0", each Table.AddIndexColumn([GlobalIdx1], "Global0", 0, 1)), "GlobalIdx0"),
Expand = Table.ExpandTableColumn(AddGlobalIdx0, "GlobalIdx0", {"Group", "GlobalOrigOrder", "Attribute", "Value", "Global1", "Global0"}, {"Group", "GlobalOri
@bjulius
bjulius / gist:b6c8adc5e5324a6dd728b3f38b4391ef
Created February 13, 2024 08:33
Excel BI Challenge 390 - Brian Julius Hybrid Power Query/R Solution
data <- dataset$Digits
find_n_digit_numbers_optimized <- function(n) {
start <- 10^(n-1)
end <- 10^n - 1
valid_numbers <- numeric(0)
for (num in start:end) {
digit_sum <- sum(as.numeric(unlist(strsplit(as.character(num), ""))))
@bjulius
bjulius / gist:6717f7139598f5954e3a55f39dd6a165
Created February 13, 2024 02:37
Owen Price Power Query Challenge Feb 12 2024 - Brian Julius Solution
let
String ="Amidst the ancient oak's shelter, the forest floor cradled fallen leaves. Leaves of gold, crimson, and russet danced in the autumn breeze. Breeze whispered secrets to the trees, and the trees listened, their branches swaying in rhythm. Rhythm of life echoed through the woodland—a symphony of rustling leaves and birdcalls. Birdcalls harmonized with the babbling brook, where water flowed over moss-covered stones. Stones, worn smooth by time, held stories of ages past. Past and present intertwined, as sunbeams filtered through the canopy, dappling the earth. Earth embraced memories, and memories lingered—a tapestry woven by seasons. Beneath the ancient oak, a squirrel gathered acorns, preparing for winter.",
Source = Web.BrowserContents("https://gist.github.com/ncalm/889ed38d780d653c4cce3d0df7f3d031"),
ExtractHTML = Html.Table(Source, {{"Column1", "TABLE.highlight.tab-size.js-file-line-container.js-code-nav-container.js-tagsearch-file > * > TR > :nth-child(1)"}, {"Column2", "TABLE.highli
@bjulius
bjulius / gist:6f3afd6604b396216f5db4fa8d8ae310
Created February 5, 2024 20:03
Owen Price Power Query Challenge Feb 5, 2024 - Brian Julius Solution
let
Source = DataRaw,
AddDupe = Table.DuplicateColumn(Source, "Entry", "EntryDupe"),
SpltToRowsByAPS = Table.ExpandListColumn(Table.TransformColumns(AddDupe, {{"EntryDupe", Splitter.SplitTextByDelimiter("APS ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "EntryDupe"),
FilterBlanks = Table.SelectRows(SpltToRowsByAPS, each ([EntryDupe] <> "")),
TrimEntryDuep = Table.TransformColumns(FilterBlanks,{{"EntryDupe", Text.Trim, type text}}),
SplitAmtByRSpace = Table.SplitColumn(TrimEntryDuep, "EntryDupe", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"EntryDupe.1", "Amount"}),
SplitDatebyRSpace = Table.RemoveColumns( Table.SplitColumn(SplitAmtByRSpace, "EntryDupe.1", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"Deposit", "Date"}), "Entry"),
AddAPS = Table.TransformColumns(SplitDatebyRSpace,{{"Deposit", each Text.Insert(_ , 0, "APS "), type text}}),
SplitBySlash = Table.SplitColumn(T
@bjulius
bjulius / gist:bce9423676a0590184a7c9defc2c4719
Created January 29, 2024 08:35
Brian Julius Solution to Crispo Mwangi January 28 2024 Power BI Challenge
RawData Query =
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcvLDQAgDALQXXr2YD9qOkvj/mtooKcXAlRJTLmjxA7wACuTOjjaIyMK+hmLU3bebKDsotPnPg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Quantity = _t]),
ReType = Table.TransformColumnTypes(Source,{{"Quantity", Int64.Type}}),
AddIndex = Table.AddIndexColumn(ReType, "Index", 1, 1, Int64.Type)
in
AddIndex
Table Query
@bjulius
bjulius / gist:422de9a47948175aff7e6983266311d5
Created January 13, 2024 09:10
Excel BI Power Query Challenge 147 – Brian Julius Solution
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ToRecord = Table.CombineColumnsToRecord(Source, "Value", Table.ColumnNames(Source)),
ToTable = Table.TransformColumns(ToRecord, {"Value", each Record.ToTable(_)}),
AddOriginalRow = Table.AddIndexColumn(ToTable, "OriginalRow", 1, 1, Int64.Type),
Expand = Table.ExpandTableColumn(AddOriginalRow, "Value", {"Name", "Value"}, {"Name", "Values"}),
AddIsPopulated = Table.AddColumn(Expand, "IsPopulated", each if [Values] = null then 0 else 1),
AddIndex = Table.AddIndexColumn(AddIsPopulated, "Index", 0, 1, Int64.Type),
AddRunningTotal = Table.AddColumn(AddIndex, "RunningTotal", each List.Sum( List.FirstN( AddIndex[IsPopulated], [Index] + 1 ))),
AddRTPrevRow = Table.AddColumn(AddIndex, "RTPrevRow", each List.Sum( List.FirstN( AddIndex[IsPopulated], [Index] ))),
@bjulius
bjulius / gist:b26af80176ea49c906fa755eb6d6cf9b
Created December 9, 2023 12:20
Excel BI Challenge 342 - Brian Julius Solution
let
Source = Table.DuplicateColumn( Excel.CurrentWorkbook(){[Name="Table1"]}[Content], "SA IDs", "ID"),
#"Sp;it1-6" = Table.SplitColumn(Source, "ID", Splitter.SplitTextByPositions({0, 6}, false), {"Date", "ID.2"}),
#"Split7-10" = Table.SplitColumn(#"Sp;it1-6", "ID.2", Splitter.SplitTextByPositions({0, 4}, false), {"SSSS", "ID.2.2"}),
Split11 = Table.SplitColumn(#"Split7-10", "ID.2.2", Splitter.SplitTextByPositions({0, 1}, false), {"Citizen", "ID.2.2.2"}),
Split12 = Table.SplitColumn(Split11, "ID.2.2.2", Splitter.SplitTextByPositions({0, 1}, false), {"Rand", "Checksum"}),
Split13 = Table.SplitColumn(Split12, "Date", Splitter.SplitTextByRepeatedLengths(2), {"Year", "Month", "Day"}),
TypeInt64 = Table.TransformColumnTypes(Split13,{{"Year", Int64.Type}, {"Month", Int64.Type}, {"Day", Int64.Type}, {"SSSS", Int64.Type}, {"Citizen", Int64.Type}, {"Rand", Int64.Type}, {"Checksum", Int64.Type}}),
AddtoYear = Table.TransformColumns(TypeInt64, {{"Year", each _ + 1900, type number}}),