Anh cho em xin code trong Power Query (Advanced Editor) được không ạ ?Power query, unpivot 4 column số tiền trả. Sau đó pivot table
View attachment 247006
View attachment 247007
Code đây. Còn thống kê theo ngày nhận khó hơn, có thể phải tạo 1 table "Ngày nhận" và join với bảng chínhAnh cho em xin code trong Power Query (Advanced Editor) được không ạ ?
Ngoài ra, em muốn thống kê theo "Ngày nhận" chứ không phải theo hóa đơn thì có thực hiện trên Query không anh?
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"No.", Int64.Type}, {"Site name", type text}, {"PO No.", type text},
{"Invoice No", type text}, {"Invoice date", type datetime}, {"Invoice value (RIG)", type number},
{"1st Received value (RIG)", type number}, {"1st Received date", type datetime}, {"2nd Received value (RIG)", Int64.Type},
{"2nd Received date", type datetime}, {"3rd Received value (RIG)", Int64.Type}, {"3rd Received date", type datetime},
{"4th Received value (RIG)", Int64.Type}, {"4th Received date", type datetime}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"No.", "Site name", "PO No.", "Invoice No", "Invoice date",
"Invoice value (RIG)", "1st Received date", "2nd Received date", "3rd Received date", "4th Received date"}, "PaySequence", "Payamount"),
#"Replaced Value" = Table.ReplaceValue(#"Unpivoted Columns"," Received value (RIG)","",Replacer.ReplaceText,{"PaySequence"})
in
#"Replaced Value"
Còn thống kê theo ngày nhận khó hơn, có thể phải tạo 1 table "Ngày nhận" và join với bảng chính
Có cách khác là unpivot 4 cột payment date, thêm 1 cột Conditional column dùng 3 If lồng để lấy PayAmountÝ của anh là tạo thêm 3 hoặc 4 bảng con trên Query, dùng "Invoice No." làm cột ID rồi cho vào power pivot sử dụng relationship giữa các bảng để tạo ạ?
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"No.", Int64.Type}, {"Site name", type text}, {"PO No.", type text},
{"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (RIG)", type number}, {"1st Received value (RIG)", type number},
{"1st Received date", type date}, {"2nd Received value (RIG)", Int64.Type}, {"2nd Received date", type date}, {"3rd Received value (RIG)",
Int64.Type}, {"3rd Received date", type date}, {"4th Received value (RIG)", Int64.Type}, {"4th Received date", type date}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"No.", "Site name", "PO No.", "Invoice No", "Invoice date",
"Invoice value (RIG)", "1st Received value (RIG)", "2nd Received value (RIG)", "3rd Received value (RIG)", "4th Received value (RIG)"},
"DateSequence", "PayDate"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"1st Received value (RIG)", "PayAmount1"}, {"2nd Received value (RIG)",
"PayAmount2"}, {"3rd Received value (RIG)", "PayAmount3"}, {"4th Received value (RIG)", "PayAmount4"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns"," Received date","",Replacer.ReplaceText,{"DateSequence"}),
#"Added Conditional Column" = Table.AddColumn(#"Replaced Value", "PaymentAmount", each if [DateSequence] = "1st" then [PayAmount1]
else if [DateSequence] = "2nd" then [PayAmount2] else if [DateSequence] = "3rd" then [PayAmount3] else [PayAmount4],type number),
#"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"PayAmount1", "PayAmount2", "PayAmount3",
"PayAmount4"})
in
#"Removed Columns"
Có cách khác là unpivot 4 cột payment date, thêm 1 cột Conditional column dùng 3 If lồng để lấy PayAmount
Mã:let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"No.", Int64.Type}, {"Site name", type text}, {"PO No.", type text}, {"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (RIG)", type number}, {"1st Received value (RIG)", type number}, {"1st Received date", type date}, {"2nd Received value (RIG)", Int64.Type}, {"2nd Received date", type date}, {"3rd Received value (RIG)", Int64.Type}, {"3rd Received date", type date}, {"4th Received value (RIG)", Int64.Type}, {"4th Received date", type date}}), #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "1st Received value (RIG)", "2nd Received value (RIG)", "3rd Received value (RIG)", "4th Received value (RIG)"}, "DateSequence", "PayDate"), #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"1st Received value (RIG)", "PayAmount1"}, {"2nd Received value (RIG)", "PayAmount2"}, {"3rd Received value (RIG)", "PayAmount3"}, {"4th Received value (RIG)", "PayAmount4"}}), #"Replaced Value" = Table.ReplaceValue(#"Renamed Columns"," Received date","",Replacer.ReplaceText,{"DateSequence"}), #"Added Conditional Column" = Table.AddColumn(#"Replaced Value", "PaymentAmount", each if [DateSequence] = "1st" then [PayAmount1] else if [DateSequence] = "2nd" then [PayAmount2] else if [DateSequence] = "3rd" then [PayAmount3] else [PayAmount4],type number), #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"PayAmount1", "PayAmount2", "PayAmount3", "PayAmount4", "DateSequence"}) in #"Removed Columns"
View attachment 247049
Thấy like từ sáng mà giờ mới thửĐể em thử theo công thức trên.
Em cảm ơn anh
Có thể dùng hàm appen table:Ngoài ra, em muốn thống kê theo "Ngày nhận" chứ không phải theo hóa đơn thì có thực hiện trên Query không anh?
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Tbl_union"=Table.Combine({
Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "1st Received value (RIG)", "1st Received date"}),
{{"1st Received value (RIG)", "Received value (RIG)"}, {"1st Received date", "Received date"}}),
Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "2nd Received value (RIG)", "2nd Received date"}),
{{"2nd Received value (RIG)", "Received value (RIG)"}, {"2nd Received date", "Received date"}}),
Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "3rd Received value (RIG)", "3rd Received date"}),
{{"3rd Received value (RIG)", "Received value (RIG)"}, {"3rd Received date", "Received date"}}),
Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "4th Received value (RIG)", "4th Received date"}),
{{"4th Received value (RIG)", "Received value (RIG)"}, {"4th Received date", "Received date"}})
}),
#"Changed Type" = Table.TransformColumnTypes(Tbl_union,{{"No.", type text}, {"Site name", type text}, {"PO No.", type text}, {"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (RIG)", type number}, {"Received value (RIG)", type number}, {"Received date", type date}})
in
#"Changed Type"
Append cách này có thể giải quyết yêu cầu thanh toán theo ngày của tác giả chủ đề, nhưng không làm được báo cáo thanh toán theo invoice như bài 6. Tất nhiên query bài 6 không phải là hay nhất hay toàn vẹn nhất, nhưng thường 1 bộ dữ liệu chuẩn sẽ ra được rất nhiều báo cáo.Có thể dùng hàm appen table
Có cột invoice và receipt date mà bạn, muốn thống kế theo chiều nào không đượcAppend cách này có thể giải quyết yêu cầu thanh toán theo ngày của tác giả chủ đề, nhưng không làm được báo cáo thanh toán theo invoice như bài 6. Tất nhiên query bài 6 không phải là hay nhất hay toàn vẹn nhất, nhưng thường 1 bộ dữ liệu chuẩn sẽ ra được rất nhiều báo cáo.
Ý tôi nói query bài 6 nhưng cho ra pivot table như cái hình ở bài 2. Chiều thứ 2 không có cái thứ tự trả (1, 2, 3, 4) để bỏ vào column của pivot table.Có cột invoice và receipt date mà bạn, muốn thống kế theo chiều nào không được
Vậy thêm 1 cột lần nữa đượcÝ tôi nói query bài 6 nhưng cho ra pivot table như cái hình ở bài 2. Chiều thứ 2 không có cái thứ tự trả (1, 2, 3, 4) để bỏ vào column của pivot table.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Tbl_union"=Table.Combine({
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "1st Received value (RIG)", "1st Received date"}),
{{"1st Received value (RIG)", "Received value (RIG)"}, {"1st Received date", "Received date"}}),"Lần",each "Lần 1"),
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "2nd Received value (RIG)", "2nd Received date"}),
{{"2nd Received value (RIG)", "Received value (RIG)"}, {"2nd Received date", "Received date"}}),"Lần",each "Lần 2"),
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "3rd Received value (RIG)", "3rd Received date"}),
{{"3rd Received value (RIG)", "Received value (RIG)"}, {"3rd Received date", "Received date"}}),"Lần",each "Lần 3"),
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name", "PO No.", "Invoice No", "Invoice date", "Invoice value (RIG)", "4th Received value (RIG)", "4th Received date"}),
{{"4th Received value (RIG)", "Received value (RIG)"}, {"4th Received date", "Received date"}}),"Lần",each "Lần 4")
}),
#"Changed Type" = Table.TransformColumnTypes(Tbl_union,{{"No.", type text}, {"Site name", type text}, {"PO No.", type text}, {"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (RIG)", type number}, {"Received value (RIG)", type number}, {"Received date", type date}, {"Lần", type text}})
in
#"Changed Type"
#"Tbl_union"=Table.Combine({
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "1st received amount - estimated", "1st received date - estimated"}),
{{"1st received amount - estimated", "Projected Received value (MMK)"}, {"1st received date - estimated", "Projected Received date"}}),"Lần",each "Lần 1"),
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "2nd received amount - estimated", "2nd received date - estimated"}),
{{"2nd received amount - estimated", "Projected Received value (MMK)"}, {"2nd received date - estimated", "Projected Received date"}}),"Lần",each "Lần 2"),
Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "3rd received amount - estimated", "3rd received date - estimated"}),
{{"3rd received amount - estimated", "Projected Received value (MMK)"}, {"3rd received date - estimated", "Projected Received date"}}),"Lần",each "Lần 3")
}),
#"Changed Type" = Table.TransformColumnTypes(Tbl_union,{{"No.", type text}, {"Site name", type text},{"Work content",type text}, {"PO No.", type text}, {"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (MMK)", type number}, {"Projected Received value (MMK)", type number}, {"Projected Received date", type date}, {"Lần", type text}}),
#"Removed Columns1" = Table.RemoveColumns(#"Changed Type",{"Invoice value (MMK)"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns1", "On Due or Overdue", each if [Projected Received date] - Date.From(DateTime.LocalNow()) < -90 then "Overdue 90+"
else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -90 and [Projected Received date] - Date.From(DateTime.LocalNow()) < -60 then "Overdue 90"
else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -60 and [Projected Received date] - Date.From(DateTime.LocalNow()) < -30 then "Overdue 60"
else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -30 and [Projected Received date] - Date.From(DateTime.LocalNow()) < 0 then "Overdue 30"
else "On Due" )
in
#"Added Custom"
Không thể so sánh trực tiếp (ngày - ngày) với 90 mà phải so sánh với #duration(90, 0, 0, 0)Đoạn hàm if lồng vào: Lấy ngày trong một cột của bảng trừ đi ngày hiện tại, so sánh với 30 60 90 lại lỗi.
Lỗi gì anh nhỉ?
if (Date.From(DateTime.LocalNow())-[PayDate]) > #duration(90,0,0,0) then "Overdue 90"
else if (Date.From(DateTime.LocalNow())-[PayDate]) > #duration(60,0,0,0) then "Overdue 60"
else if (Date.From(DateTime.LocalNow())-[PayDate]) > #duration(30,0,0,0) then "Overdue 30"
else "On due"
Bạn chỉnh lại source mà không đưa dữ liệu lên sao biết sai chỗ nào bạnEm đã tùy biến và chỗ đưa về một cột đã được rồi ạ. Nhưng em thêm if để phân loại thì lại lỗi
Mã:#"Tbl_union"=Table.Combine({ Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "1st received amount - estimated", "1st received date - estimated"}), {{"1st received amount - estimated", "Projected Received value (MMK)"}, {"1st received date - estimated", "Projected Received date"}}),"Lần",each "Lần 1"), Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "2nd received amount - estimated", "2nd received date - estimated"}), {{"2nd received amount - estimated", "Projected Received value (MMK)"}, {"2nd received date - estimated", "Projected Received date"}}),"Lần",each "Lần 2"), Table.AddColumn(Table.RenameColumns(Table.SelectColumns(Source,{"No.", "Site name","Work content", "PO No.", "Invoice No", "Invoice date", "Invoice value (MMK)", "3rd received amount - estimated", "3rd received date - estimated"}), {{"3rd received amount - estimated", "Projected Received value (MMK)"}, {"3rd received date - estimated", "Projected Received date"}}),"Lần",each "Lần 3") }), #"Changed Type" = Table.TransformColumnTypes(Tbl_union,{{"No.", type text}, {"Site name", type text},{"Work content",type text}, {"PO No.", type text}, {"Invoice No", type text}, {"Invoice date", type date}, {"Invoice value (MMK)", type number}, {"Projected Received value (MMK)", type number}, {"Projected Received date", type date}, {"Lần", type text}}), #"Removed Columns1" = Table.RemoveColumns(#"Changed Type",{"Invoice value (MMK)"}), #"Added Custom" = Table.AddColumn(#"Removed Columns1", "On Due or Overdue", each if [Projected Received date] - Date.From(DateTime.LocalNow()) < -90 then "Overdue 90+" else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -90 and [Projected Received date] - Date.From(DateTime.LocalNow()) < -60 then "Overdue 90" else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -60 and [Projected Received date] - Date.From(DateTime.LocalNow()) < -30 then "Overdue 60" else if [Projected Received date] - Date.From(DateTime.LocalNow()) >= -30 and [Projected Received date] - Date.From(DateTime.LocalNow()) < 0 then "Overdue 30" else "On Due" ) in #"Added Custom"
Đoạn hàm if lồng vào: Lấy ngày trong một cột của bảng trừ đi ngày hiện tại, so sánh với 30 60 90 lại lỗi.
Lỗi gì anh nhỉ?
Nếu thấy hàm M khó và muốn trốn tránh thì load to - Add to Data model và dùng công thức excel cho cột mớiEm đã tùy biến và chỗ đưa về một cột đã được rồi ạ. Nhưng em thêm if để phân loại thì lại lỗi
=if(now()-DailyPay[PayDate]>90,"OverDue90",if(now()-DailyPay[PayDate]>60,"OverDue60",if(now()-DailyPay[PayDate]>30,"OverDue30","Ondue")))
Có thể viết vầy cũng được:Nếu thấy hàm M khó và muốn trốn tránh thì load to - Add to Data model và dùng công thức excel
PHP:=if(now()-DailyPay[PayDate]>90,"OverDue90",if(now()-DailyPay[PayDate]>60,"OverDue60",if(now()-DailyPay[PayDate]>30,"OverDue30","Ondue")))
=
Var cal1=now()-DailyPay[PayDate]
Return
swicth(true(),
cal1>90,"OverDue90",
cal1>60,"OverDue60",
cal1>30,"OverDue30",
"Ondue")
Đối với 1 số người thì if then else if then else như bài #15 đã là khó rồi, đụng vô var và switch lại còn khó hơnCó thể viết vầy cũng được:
Mã:= Var cal1=now()-DailyPay[PayDate] Return swicth(true(), cal1>90,"OverDue90", cal1>60,"OverDue60", cal1>30,"OverDue30", "Ondue")
Coi lại bài 15 để biết sai ở đâuCảm ơn anh @ptm0412 và anh @excel_lv1.5