Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sjtalkar/b8b757b1b0de29ed629fa203ce736dcb to your computer and use it in GitHub Desktop.
Save sjtalkar/b8b757b1b0de29ed629fa203ce736dcb to your computer and use it in GitHub Desktop.
let
Source = Odbc.DataSource("dsn=CData SugarCRM Sys", [HierarchicalNavigation=true]),
CData_Database = Source{[Name="CData",Kind="Database"]}[Data],
SugarCRM_Schema = CData_Database{[Name="SugarCRM",Kind="Schema"]}[Data],
Accounts_Table = SugarCRM_Schema{[Name="Accounts",Kind="Table"]}[Data],
table_to_work_with = Table.SelectColumns(Accounts_Table,{"Id", "Tag"}),
tags_table = Table.TransformColumnTypes(table_to_work_with,{{"Tag", type text}}),
split_by_colon = Table.SplitColumn(tags_table, "Tag", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Tag.1", "Tag.2", "Tag.3", "Tag.4", "Tag.5", "Tag.6", "Tag.7", "Tag.8", "Tag.9", "Tag.10", "Tag.11", "Tag.12", "Tag.13", "Tag.14", "Tag.15", "Tag.16", "Tag.17"}),
tag_cols = List.Range (Table.ColumnNames(split_by_colon) , 2),
get_id_cols = List.Generate(() => [counter = 0, x = tag_cols, result="False"] ,
each [counter] < List.Count([x]),
each [counter=[counter]+1 ,
x= tag_cols,
result= if List.Count( List.FindText(Table.Column(split_by_colon, [x]{counter}), "ACB")) > 0
or List.Count( List.FindText(Table.Column(split_by_colon, [x]{counter}), "BOS")) > 0
or List.Count( List.FindText(Table.Column(split_by_colon, [x]{counter}), "NDL")) > 0
then "True" else "False"
],
each [result] ),
pos_list = List.PositionOf( get_id_cols, "True", Occurrence.All),
list_of_tags = List.Transform( pos_list, each tag_cols{_}),
create_tags_table = Table.SelectColumns(split_by_colon, List.Combine({{"Id"}, list_of_tags})),
remove_curly_braces = Table.ReplaceValue(create_tags_table,"}","",Replacer.ReplaceText,list_of_tags),
replace_id = Table.ReplaceValue(remove_curly_braces,",id","",Replacer.ReplaceText,list_of_tags),
trim_text = Table.TransformColumns(replace_id, {}, Text.Trim),
replace_nulls = Table.ReplaceValue(trim_text,null,"",Replacer.ReplaceValue,list_of_tags),
fnreplaceNull = (cellText as text) =>
let
result = if cellText = null then ""
else cellText
in
result,
fnPowerTrim = (cellText as text) =>
let
result = if cellText = null then ""
else if Text.StartsWith(cellText, "ACB") or Text.StartsWith(cellText, "BOS") or Text.StartsWith(cellText, "NDL")
then cellText else ""
in
result,
last_table = Table.Buffer(replace_nulls),
final_table_list = List.Generate(
() => [counter=0, x=List.Combine({{"Id"}, list_of_tags}), result = last_table ],
each [counter] < List.Count([x]),
each [counter=[counter]+1 ,
x= List.Combine({{"Id"}, list_of_tags}),
result= Table.TransformColumns([result], {x{counter}, fnPowerTrim, type text})
],
each [result] ),
final_table = List.Last( final_table_list),
join_columns = Table.CombineColumns(final_table, list_of_tags , Combiner.CombineTextByDelimiter(",", QuoteStyle.None), "Tag"),
replace_successive_commas = Table.ReplaceValue(join_columns,",,","",Replacer.ReplaceText,{"Tag"}),
tgs_containing_ACB = Table.SelectRows (replace_successive_commas, each Text.Length([Tag]) > 1),
#"Trimmed Text" = Table.TransformColumns(tgs_containing_ACB,{{"Tag", each Text.TrimEnd(_,","), type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Trimmed Text",{{"Tag", "Combined Tag"}})
in
#"Renamed Columns"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment