From f8b84fc94f91651f3e0b0e242a604762c7c67c5c Mon Sep 17 00:00:00 2001 From: DaTekShaman Date: Wed, 30 Jul 2025 21:57:02 +0300 Subject: [PATCH] Updates --- power-query/fP.m | 138 +++++++++++++++++++++++++++++++++++++++++ power-query/fReplace.m | 19 ++++++ 2 files changed, 157 insertions(+) create mode 100644 power-query/fP.m create mode 100644 power-query/fReplace.m diff --git a/power-query/fP.m b/power-query/fP.m new file mode 100644 index 0000000..5266178 --- /dev/null +++ b/power-query/fP.m @@ -0,0 +1,138 @@ +(P_NAME as text, optional P_FORCED_TYPE as nullable text) => let + + //Получаем параметр из таблицы с настройками + GET_PARAMETER = try + let + GetTable = Excel.CurrentWorkbook(){[Name="PARAMETERS_TABLE"]}[Content], + KeepNeeded = Table.SelectColumns(GetTable,{"ПАРАМЕТР", "ЗНАЧЕНИЕ","ТИП"}), + SetTypes = Table.TransformColumnTypes(KeepNeeded,{{"ПАРАМЕТР", type text}, {"ЗНАЧЕНИЕ", type any}, {"ТИП", type text}}), + Filter = Table.SelectRows(SetTypes, each ([ПАРАМЕТР] = P_NAME)), + Value = Table.SingleRow(Filter) + in + Value otherwise null, + + PATH_OVERRIDE = try if P_NAME = "PATH_OVERRIDE" then null else fP("PATH_OVERRIDE") otherwise null, + + P_TYPE = if GET_PARAMETER <> null then + if P_FORCED_TYPE = null then GET_PARAMETER[ТИП] else P_FORCED_TYPE + else + null, + + P_VALUE = if GET_PARAMETER <> null then + if PATH_OVERRIDE <> null and + List.Contains({"FILE", "EXCEL", "DIR", "PATH", "CSVL"}, P_TYPE) then + fReplaceFunction(GET_PARAMETER[ЗНАЧЕНИЕ], false, PATH_OVERRIDE ) + else + GET_PARAMETER[ЗНАЧЕНИЕ] + else + null, + + // Определяем функции + // БУЛЕВО + F_Boolean = (Input as any) => let + Result = Value.ReplaceType(Input, type logical) + in + Result, + + // ЦЕЛОЕ ЧИСЛО + F_Integer = (Input as any) => let + Result = Value.ReplaceType(Input, Int64.Type) + in + Result, + + // ЧИСЛО С ЗАПЯТОЙ + F_Float = (Input as any) => let + Result = Value.ReplaceType(Input, type number) + in + Result, + + // СТРОКА + F_Text = (Input as any) => let + Result = Value.ReplaceType(Input, type text) + in + Result, + + // ДАТА + F_Date = (Input as any) => let + Result = Value.ReplaceType(DateTime.Date(Input), type date) + in + Result, + + // ДАТА И ВРЕМЯ + F_DateTime = (Input as any) => let + Result = Value.ReplaceType(Input, type datetime) + in + Result, + + // СПИСОК В ФОРМАТЕ PQ + F_List = (Input as any) => let + Result = Text.Split(Text.Replace(Text.From(Input), "; ", ";"), ";") + in + Result, + + // ТАБЛИЦА В ФОРМАТЕ PQ + F_RcrdSet = (Input as any) => let + RemoveSpaces = Text.Replace(Text.Replace(Input, " ;", ";"), "; ", ";"), + SplitRows = Text.Split(RemoveSpaces, "};{"), + Replace = List.ReplaceValue(List.ReplaceValue(SplitRows,"}","",Replacer.ReplaceText),"{","",Replacer.ReplaceText), + Result = Table.FromList(Replace, Splitter.SplitTextByDelimiter(";")) + in + Result, + + // СПИСОК ЗАМЕН + F_Symlink = (Input as any) => let + RemoveSpaces = Text.Replace(Text.Replace(Input, " ;", ";"), "; ", ";"), + SplitRows = Text.Split(RemoveSpaces, ";"), + Result = Table.FromList(SplitRows, Splitter.SplitTextByDelimiter(">>")) + in + Result, + + // ФАЙЛ EXCEL + F_ExcelFile = (Input as any) => let + Result = Excel.Workbook(File.Contents(Input), null, true) + in + Result, + + // СОДЕРЖИМОЕ ПАПКИ + F_DirContent = (Input as any) => let + Result = Folder.Files(Input) + in + Result, + + // ЛОКАЛЬНАЯ ИМЕНОВАННАЯ ТАБЛИЦА + F_LocalTable = (Input as any) => let + Result = Excel.CurrentWorkbook(){[Name=Input]}[Content] + in + Result, + + // CSV-ФАЙЛ В СТАНДАРТНОЙ ПАПКЕ + F_LocalCSV = (Input as any) => let + Result = Csv.Document(File.Contents("\\msk.mts.ru\msk\WORKDATA\OPSIMPAO-ANALYTICS\Aggregated CSV\" & Input & ".csv"), [Delimiter=",", Encoding=1251, QuoteStyle=QuoteStyle.None]) + in + Result, + + CaseValues = { + { List.Contains({"BOOL" , "LOGICAL" }, P_TYPE), try F_Boolean (P_VALUE) otherwise null}, + { List.Contains({"INT" , "INT64" }, P_TYPE), try F_Integer (P_VALUE) otherwise null}, + { List.Contains({"FLOAT" , "DOUBLE" }, P_TYPE), try F_Float (P_VALUE) otherwise null}, + { List.Contains({"TEXT" , "STRING" }, P_TYPE), try F_Text (P_VALUE) otherwise null}, + { List.Contains({"DATE" }, P_TYPE), try F_Date (P_VALUE) otherwise null}, + { List.Contains({"DATETIME", "DTTM" }, P_TYPE), try F_DateTime (P_VALUE) otherwise null}, + + { List.Contains({"LIST" }, P_TYPE), try F_List (P_VALUE) otherwise null}, + { List.Contains({"RCRDSET", "PQTABLE" }, P_TYPE), try F_RcrdSet (P_VALUE) otherwise null}, + { List.Contains({"SYMLNK" , "SYMLINK" }, P_TYPE), try F_Symlink (P_VALUE) otherwise null}, + + { List.Contains({"TABLE" }, P_TYPE), try F_LocalTable (P_VALUE) otherwise null}, + + { List.Contains({"FILE" , "EXCEL" }, P_TYPE), try F_ExcelFile (P_VALUE) otherwise null}, + { List.Contains({"DIR" , "PATH" }, P_TYPE), try F_DirContent (P_VALUE) otherwise null}, + { List.Contains({"CSVL" }, P_TYPE), try F_LocalCSV (P_VALUE) otherwise null}, + + { true , P_VALUE } + }, + + SelectCase = List.First(List.Select(CaseValues, each _{0} = true)){1} + + in + SelectCase \ No newline at end of file diff --git a/power-query/fReplace.m b/power-query/fReplace.m new file mode 100644 index 0000000..ae68aba --- /dev/null +++ b/power-query/fReplace.m @@ -0,0 +1,19 @@ +let + ReplaceFunction = ( P_INPUT as nullable text, + optional P_IS_WHOLE_WORD as nullable logical, + optional P_SUBSTITUTION as nullable table) as text => + let + isWhole = if (P_IS_WHOLE_WORD = null) then false else P_IS_WHOLE_WORD, + SubstitutionTable = if P_SUBSTITUTION = null then fP("RENAMING_RULES", "TABLE") else P_SUBSTITUTION, + ReplacedText = List.Accumulate(Table.ToRows(SubstitutionTable), P_INPUT, (state, substitution) => + if isWhole then + if (state = substitution{0}) then + substitution{1} + else + state + else + Text.Replace(state, substitution{0}, substitution{1})) + in + ReplacedText +in + ReplaceFunction \ No newline at end of file