(таблица,группа,столбцы)=>
[ g=(x)=>[a=Record.ToList(Record.SelectFields(x,столбцы)),
b=Record.Field(dict,Record.Field(x,группа)),
c=List.Transform(List.Zip({a,b}),(x)=>if x{0}>=x{1}{0} then "A" else if x{0}>=x{1}{1} then "B" else "C")][c],
f=(x)=>(y)=>[a=List.Buffer(Table.Column(y,x)),
b=List.Average(a),
c={b,List.Average(List.Select(a,(x)=>x<b))}][c],
gr = Table.Group(таблица,группа,List.Transform(столбцы,(x)=>{x,f(x)})),
dict = Record.FromList(Table.ToList(gr,List.Skip),Table.Column(gr,группа)),
add = Table.AddColumn(таблица,"tmp",g),
nms = List.Transform(столбцы,(x)=>"ABC_"&x),
to = Table.SplitColumn(add,"tmp",(x)=>x,nms)][to]
let
f=(x)=>{BinaryFormat.UnsignedInteger32(Binary.Range(x,16,4)),BinaryFormat.UnsignedInteger32(Binary.Range(x,20,4))},
from = Folder.Files("C:\Users\muzyk\Desktop\PQ КУРС МАТЕРИАЛЫ"),
filtr = Table.SelectRows(from,(x)=>x[Extension]=".png")[[Name],[Content]],
to = Table.SplitColumn(filtr,"Content",f,{"Width","Height"})
in
to
let
f=(x)=>List.Skip(BinaryFormat.List(BinaryFormat.UnsignedInteger32,6)(x),4),
from = Folder.Files("C:\Users\muzyk\Desktop\PQ КУРС МАТЕРИАЛЫ"),
filtr = Table.SelectRows(from,(x)=>x[Extension]=".png")[[Name],[Content]],
to = Table.SplitColumn(filtr,"Content",f,{"Width","Height"})
in
to
let
f=BinaryFormat.Record(
[skip=BinaryFormat.Binary(16),
Width=BinaryFormat.UnsignedInteger32,
Height=BinaryFormat.UnsignedInteger32]),
from = Folder.Files("C:\Users\muzyk\Desktop\PQ КУРС МАТЕРИАЛЫ"),
filtr = Table.SelectRows(from,(x)=>x[Extension]=".png")[[Name],[Content]],
tr = Table.TransformColumns(filtr,{"Content",f}),
to = Table.ExpandRecordColumn(tr,"Content",{"Width","Height"})
in
to
let
Источник = Excel.CurrentWorkbook(){[Name="Sales2"]}[Content],
#"Объединенные запросы" = Table.NestedJoin(Источник, {"Service ID"}, dimCost, {"Service ID"}, "dimCost", JoinKind.LeftOuter),
#"Развернутый элемент dimCost" = Table.ExpandTableColumn(#"Объединенные запросы", "dimCost", {"Cost"}, {"Cost"}),
#"Замененное значение" = Table.ReplaceValue(#"Развернутый элемент dimCost",null,0,Replacer.ReplaceValue,{"Cost"})
in
#"Замененное значение"
let
base = Excel.CurrentWorkbook(){[Name="dimCost2"]}[Content],
dict = Record.FromList(base[Cost],List.Transform(base[Service ID],Text.From)),
from = Excel.CurrentWorkbook(){[Name="Sales2"]}[Content],
to = Table.AddColumn(from,"Cost",(x)=>Record.FieldOrDefault(dict,Text.From(x[Service ID]),0))
in
to
(таблица,группа,столбцы)=>
let
ABC=(tbl,col,colname)=>[
t = Table.Buffer(Table.Sort(tbl,{col,Order.Descending})),
r = List.Buffer(Table.Column(t,col)),
n = List.Count(r),s = List.Sum(r),
a = 0.5*s,b = 0.8*s,c = 0.95*s,
f=(x)=>if x<=a then "A" else if x<=b then "B" else if x<=c then "C" else "D",
g = List.Generate(()=>[i=0,j=r{i},k="A"],(x)=>x[i]<n,(x)=>[i=x[i]+1,j=r{i}+x[j],k=f(j)],(x)=>x[k]),
res = Table.FromColumns(Table.ToColumns(t)&{g},Table.ColumnNames(t)&{colname})][res],
add = Table.AddIndexColumn(таблица,"i"),
lst = List.Buffer(List.Transform(столбцы,(x)=>{x,"ABC_"&x})),
g=(tbl)=>List.Accumulate(lst,tbl,(s,c)=>ABC(s,c{0},c{1})),
gr=Table.Group(add,группа,{"t",g}),
exp = Table.ExpandTableColumn(gr,"t",List.Combine(lst)&{"i"}),
to = Table.RemoveColumns(Table.Sort(exp,"i"),"i")
in
to
(таблица,группа,столбцы)=>
[
ABC=(tbl,col,colname)=>[
t = Table.Buffer(Table.Sort(tbl,{col,Order.Descending})),
r = List.Buffer(Table.Column(t,col)),
n = List.Count(r),s = List.Sum(r),
a = g(0.5*s), b = g(0.8*s), c = g(0.95*s),
f=(x)=>if x<=a or x=0 then "A" else if x<=b then "B" else if x<=c then "C" else "D",
gen = List.Buffer(List.Generate(()=>[i=0,j=r{i}],(x)=>x[i]<n,(x)=>[i=x[i]+1,j=r{i}+x[j]],(x)=>x[j])),
g=(x)=>List.PositionOf(gen,x,Occurrence.Last,(c,v)=>v>=c),
res = Table.TransformColumns(Table.AddIndexColumn(t,colname),{colname,f})][res],
add = Table.AddIndexColumn(таблица,"i"),
lst = List.Buffer(List.Transform(столбцы,(x)=>{x,"ABC_"&x})),
g=(tbl)=>List.Accumulate(lst,tbl,(s,c)=>ABC(s,c{0},c{1})),
gr=Table.Group(add,группа,{"t",g}),
exp = Table.Combine(gr[t]),
to = Table.RemoveColumns(Table.Sort(exp,"i"),"i")][to]
let
f=(x)=>Function.Invoke(Record.FromList,List.Reverse(List.Zip(List.Split(List.RemoveNulls(x),2)))),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="TData"]}[Content]),
nms = List.Select(Table.ColumnNames(from),(x)=>Text.Contains(x,"DL")),
cmb = Table.Buffer(Table.CombineColumns(from,nms,f,"tmp")),
newnms = List.Distinct(List.Combine(List.Transform(cmb[tmp],Record.FieldNames))),
to = Table.ExpandRecordColumn(cmb,"tmp",newnms)
in
to
let
f=(x)=>[a=List.Zip(List.Split(List.RemoveNulls(List.Skip(x,pos)),2)),
b=Record.FromList(List.FirstN(x,pos)&a{1},fnms&a{0})][b],
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="TData"]}[Content]),
nms = Table.ColumnNames(from),
pos = List.PositionOf(nms,"DL",Occurrence.First,Text.Contains),
fnms = List.Buffer(List.FirstN(nms,pos)),
lst = List.Buffer(Table.ToList(from,f)),
newnms = List.Distinct(List.Combine(List.Transform(lst,Record.FieldNames))),
to = Table.FromRecords(lst,newnms,MissingField.UseNull)
in
to
let
from = List.Distinct(Excel.CurrentWorkbook(){[Name="база"]}[Content][Наименование]),
lst = List.Buffer(List.Transform(List.Zip({from,{1..List.Count(from)}}),(x)=>{x{0},Text.ToList(Text.Upper(x{0})),Text.Length(x{0}),x{1}})),
g=(x,y)=>2*List.Count(List.Intersect({x{1},y{1}}))/(x{2}+y{2}),
f=(x)=>List.Select(List.Transform(List.Skip(lst,x{3}),(y)=>{x{0},y{0},g(x,y)}),(z)=>z{2}>0.86),
tr = List.TransformMany(lst,f,(x,y)=>y),
to = Table.FromList(tr,(x)=>x,{"Наименование","Похожее","Подобие"})
in
to
let
f = (x)=>[ a = "Column"&Text.From(List.Last(Record.ToList(x{0}))),
b = Table.SelectRows(base,g(x)),
c = List.Sum(Table.Column(b,a))][c],
g = (x)=>[ a = List.Split(List.RemoveLastN(List.Skip(Table.ToColumns(x)),1),3),
b = (x)=>if x{2}{0}=null then null else "("&Text.Combine(List.Transform(List.Distinct(x{2}),c(x))," or ")&")",
c = (x)=>(y)=>Text.Format("(x[Column#{0}]#{1}""#{2}"")",{x{0}{0},x{1}{0},y}),
d = Expression.Evaluate("(x)=>"&Text.Combine(List.Transform(a,b)," and "))][d],
base = Table.Buffer(Excel.CurrentWorkbook(){[Name="Исходные"]}[Content]),
from = Excel.CurrentWorkbook(){[Name="Таблица2"]}[Content],
to = Table.Group(from, "Строка итога", {"Сумма",f})
in
to
let
lst = List.Buffer(Excel.CurrentWorkbook(){[Name="artlist"]}[Content][Value]),
dict = Record.FromList(List.Repeat({true},List.Count(lst)),lst),
f=(x)=>Record.FieldOrDefault(dict,x[#"Артикул (продукции)"],false),
g=(x)=>not f(x),
h=(x)=>Table.SelectRows(from,x),
from = Table.Buffer(Excel.CurrentWorkbook(){[Name="combatset"]}[Content]),
nms = List.Buffer({"Реквизиты спецификации"}&List.LastN(Table.ColumnNames(from),4)),
dict1 = Record.FromTable(Table.RenameColumns(Table.Group(h(f),"Партия (продукции)",{"Value",(x)=>Table.SelectColumns(x,nms&{"Сдано на склад"})}),{"Партия (продукции)","Name"})),
j=(x)=>[a=Record.FieldOrDefault(dict1,x{3}),
b=List.Last(x),
c=Table.ToList(a,(x)=>List.FirstN(x,4)&{x{4}/x{5}*b}),
d=if a = null then {x} else c][d],
cmb = Table.CombineColumns(h(g),nms,j,"tmp"),
exp = Table.ExpandListColumn(cmb,"tmp"),
to = Table.SplitColumn(exp,"tmp",(x)=>x,nms)
in
to
let
lst = Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col],
dict = List.Buffer(Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match]),
to = List.Select(lst,(x)=>List.Contains(dict,x,(y)=>Text.StartsWith(x,y)))
in
to
let
lst = List.Buffer(Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col]),
dict = List.Buffer(Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match]),
to = List.Transform(List.PositionOfAny(lst,dict,Occurrence.All,(c,v)=>Text.StartsWith(c,v)),(x)=>lst{x})
in
to
let
lst = List.Buffer(Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col]),
dict = List.Buffer(Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match]),
to = List.Transform(List.PositionOfAny(lst,dict,Occurrence.All,Text.StartsWith),(x)=>lst{x})
in
to
let
lst = Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col],
dict = List.Buffer(Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match]),
to = List.Select(lst,(x)=>Splitter.SplitTextByAnyDelimiter(dict)(x){0}="")
in
to
let
lst = Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col],
dic = Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match],
len = List.Buffer(List.Distinct(List.Transform(dic,Text.Length))),
dict = Record.FromList(List.Repeat({true},List.Count(dic)),dic),
f=(x)=>List.AnyTrue(List.Transform(len,(y)=>Record.FieldOrDefault(dict,Text.Start(x,y)))),
to = List.Select(lst,f)
in
to
let
lst = Excel.CurrentWorkbook(){[Name="tsrc"]}[Content][col],
dic = Excel.CurrentWorkbook(){[Name="tmatch"]}[Content][match],
len = List.Buffer(List.Distinct(List.Transform(dic,(x)=>{0,Text.Length(x)}))),
dict = Record.FromList(List.Repeat({true},List.Count(dic)),dic),
f=(x)=>List.AnyTrue(List.Transform(Splitter.SplitTextByRanges(len)(x),(y)=>Record.FieldOrDefault(dict,y))),
to = List.Select(lst,f)
in
to
let
from = Sql.Databases(BASE){[Name="ReportServerPBI"]}[Data]{[Schema="dbo",Item="ExecutionLog3"]}[Data],
dt = Date.StartOfDay(DateTime.LocalNow()),
filtr = Table.SelectRows(from, each ([ItemAction] = "DataRefresh") and ([Status] = "rsInternalError") and [TimeEnd]>dt),
cols = Table.SelectColumns(filtr,{"ItemPath","TimeStart","TimeEnd"}),
sort = Table.Sort(cols,"TimeEnd"),
lst = Table.ToList(sort,(x)=>Text.Format("❌ #{0}#(lf)с #{1}#(lf)по #{2}#(lf)",x)),
txt = Text.Replace(Text.Combine(lst,"#(lf)"),"_","\_"),
post = try Json.Document(Web.Contents("https://api.telegram.org/bot"&TOKEN&"/sendMessage", [Query = [chat_id=ID,text=txt, parse_mode = "Markdown"], Content = Text.ToBinary("")]))[ok] otherwise false ,
tab = #table(1, {{post}})
in
tab
let
f=(x)=>[z1=List.PositionOf(search,x,Occurrence.First,(c,v)=>Text.Contains(v,c)),
z2=if z1<>-1 then res{z1} else null][z2],
dict=Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content],
search = List.Buffer(dict[Назначение платежа]),
res = List.Buffer(dict[Статья расходов]),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
f=(x)=>((a)=>res{List.PositionOf(search,a,Occurrence.First,(c,v)=>Text.Contains(v,c))})(Text.Lower(x)),
dict=Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content],
search = List.Buffer(dict[Назначение платежа]&{""}),
res = List.Buffer(dict[Статья расходов]&{null}),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
to = Table.TransformColumns(from,{"Назначение платежа",f})
in
to
let
f=(x)=>List.Skip(dict,(y)=>not Text.Contains(x,y{0})){0}?{1}?,
dict=List.Buffer(Table.ToList(Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content],(x)=>x)),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
f=(x)=>((z)=>List.Skip(dict,(y)=>not Text.Contains(z,y{0})){0}?{1}?)(Text.Lower(x)),
dict=List.Buffer(Table.ToList(Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content],(x)=>x)),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
to = Table.TransformColumns(from,{"Назначение платежа",f})
in
to
let
f=(x)=>Text.Split(List.Accumulate(dict,x,(s,c)=>Text.Replace(s,c[Назначение платежа],c[Статья расходов])),"!"){1}?,
dict = List.Buffer(Table.ToRecords(Table.TransformColumns(Excel.CurrentWorkbook(){[Name="справочник"]}[Content],{"Статья расходов",(x)=>"!"&x&"!"}))),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="данные"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
f=(x)=>Text.Split(List.Accumulate(dict,[a=x,b=false],(s,c)=>if s[b] then s else if Text.Contains(s[a],c[Назначение платежа]) then [a=c[Статья расходов],b=true] else s)[a],"!"){1}?,
dict=List.Buffer(Table.ToRecords(Table.TransformColumns(Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content], {"Статья расходов", (x)=>"!"&x&"!"}))),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
f=(x)=>[a=List.Accumulate(dict,[a=x,b=false],(s,c)=>if s[b] then s else if Text.Contains(s[a],c[Назначение платежа]) then [a=c[Статья расходов],b=true] else s),
b=if a[b] then a[a] else null][b],
dict=List.Buffer(Table.ToRecords(Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content])),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
f=(x)=>List.Skip(dict,(y)=>not Text.Contains(x,y{0})){0}?{1}?,
dict=List.Buffer(Table.ToList(Excel.CurrentWorkbook(){[Name="Таблица3"]}[Content],(x)=>x)),
from = Table.PromoteHeaders(Excel.CurrentWorkbook(){[Name="data"]}[Content])[[Дата проведения],[Сумма в валюте счёта],[Назначение платежа]],
tr = Table.TransformColumns(from,{"Назначение платежа",Text.Lower}),
to = Table.TransformColumns(tr,{"Назначение платежа",f})
in
to
let
from = Excel.CurrentWorkbook(){[Name="ЕСТЬ"]}[Content],
lst = List.Buffer(Table.ToList(from,(x)=>x)),
n=List.Count(lst),
gen = List.Generate(()=>[i=0,j=lst{i},k=0,l=j],
(x)=>x[i]<n,
(x)=>[i=x[i]+1,j=lst{i},
k=List.PositionOf(j,null,Occurrence.First,(c,v)=>c<>v),
l=List.FirstN(x[l],k)&List.Skip(j,k)],
(x)=>x[l]),
to = Table.FromList(gen,(x)=>x,Value.Type(from))
in
to
let
f=(x)=>
[lst = List.Buffer(Table.ToList(x,(x)=>x)),
n=List.Count(lst),
gen = List.Generate(()=>[i=0,k=0,l=lst{i}],
(x)=>x[i]<n,
(x)=>[i=x[i]+1,
k=List.PositionOf(lst{i},null,Occurrence.First,(c,v)=>c<>v),
l=List.ReplaceRange(lst{i},0,k,List.FirstN(x[l],k))],
(x)=>x[l])][gen],
from = Excel.CurrentWorkbook(){[Name="ЕСТЬ"]}[Content],
gr = Table.Group(from,"Path 1",{"tmp",f},GroupKind.Local,(s,c)=>Number.From(c<>null)),
to = Table.FromList(List.Combine(gr[tmp]),(x)=>x,Value.Type(from))
in
to
let
f=(x)=>Table.ToList(x,(x)=>x),
g=(x)=>[a=List.PositionOf(x{0},null,Occurrence.First,(c,v)=>c<>v),
b=List.ReplaceRange(x{0},0,a,List.FirstN(x{1},a))][b],
h=(x)=>List.Zip({f(x),f(Table.FillDown(x,nms))}),
from = Excel.CurrentWorkbook(){[Name="ЕСТЬ"]}[Content],
nms = List.RemoveLastN(Table.ColumnNames(from),3),
gr = Table.Group(from,"Path 1",{"tmp",h},GroupKind.Local,(s,c)=>Number.From(c<>null)),
to = Table.FromList(List.Combine(gr[tmp]),g,Value.Type(from))
in
to
let
f=(x)=>[a=dm(x{0},x{1}),
b=Duration.TotalHours(Date.EndOfMonth(x{0})-x{0}),
c=Duration.TotalHours(x{1}-Date.StartOfMonth(x{1})),
d=ym(x{0})-num,
e=x&List.Repeat({null},d),
f=if a=0 then e&{Duration.TotalHours(x{1}-x{0})}
else if a=1 then e&{b,c}
else e&{b}&List.Range(hrs,d+1,a-1)&{c}][f],
h=(x,y,z)=>if x>y then z else @h(Date.AddMonths(x,1),y,z&{x}),
ym=(x)=>Date.Year(x)*12+Date.Month(x),
dm=(x,y)=>ym(y)-ym(x),
from = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ot = Date.StartOfMonth(List.Min(from[Начало работ])),
num = ym(ot),
do = List.Max(from[Завершение работ]),
lst = List.Buffer(h(ot,do,{})),
nms = Table.ColumnNames(from)&List.Transform(lst,(x)=>DateTime.ToText(x,"yyyyMM")),
hrs = List.Buffer(List.Transform(lst,(x)=>Duration.TotalHours(Date.EndOfMonth(x)-x))),
to = Table.FromList(Table.ToList(from,f),(x)=>x,nms)
in
to