Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let
- GetResults = (z as text, x as number) =>
- let
- S = Json.Document(Web.Contents(ServiceRootURL & "/leads", [Headers=[Prefer="odata.include-annotations=*"],Query=[fetchXml="
- <fetch page=""" & Text.From(x) & """ paging-cookie=""" & z & """>
- <entity name=""lead""> <all-attributes />
- </entity>
- </fetch>"]])),
- P = try Xml.Document(S[#"@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"]) otherwise null,
- R = if P <> null
- then List.Combine({S[value],@GetResults(Text.Replace(Text.Replace(Text.Replace(Uri.Parts("http://a.b?d=" & Uri.Parts("http://a.b?d=" & P{0}[Attributes]{1}[Value])[Query][d])[Query][d], ">", ">"), "<", "<"), """", """), x + 1)})
- else S[value]
- in
- R,
- ResultsList = GetResults("", 1),
- ResultsTable = if List.IsEmpty(ResultsList)
- then #table(
- type table[ #"Lead ID"= text,
- #"Created On"= datetimezone,
- #"Owner (ownerid)"= text,
- #"Owner (Type)"= text,
- #"Owner"= text,
- #"Country of sale (ccs_country_of_sale)"= text,
- #"Country of sale"= text,
- #"Line of Business (ccs_lineofbusiness)"= text,
- #"Line of Business"= text,
- #"Account (ccs_account)"= text,
- #"Account"= text,
- #"Products (ccs_products)"= text,
- #"Products"= text,
- #"Annual Recurring Revenue"= number,
- #"ARR in USD"= text,
- #"ARR in USD(Formatted Value)"= text,
- #"Target Margin% (ccs_targetmargin)"= text,
- #"Target Margin%"= text,
- #"Stage Name"= text,
- #"Closed Status (ccs_closedstatus)"= text,
- #"Closed Status"= text,
- #"Lead Source (leadsourcecode)"= text,
- #"Lead Source"= text,
- #"Head Count"= number,
- #"Expected Closure Date"= date,
- #"Target Go Live Quarter (ccs_target_go_live_quarter)"= text,
- #"Target Go Live Quarter"= text,
- #"Target Go Live Period (ccs_target_go_live_period)"= text,
- #"Target Go Live Period"= text,
- #"Modified By (modifiedby)"= text,
- #"Modified By"= text,
- #"Modified On"= datetimezone,
- #"Latest Update"= text,
- #"Agreement Closure Date"= date,
- #"Implementation Fee Charged (ccs_implementationfee)"= text,
- #"Implementation Fee Charged"= text,
- #"Implementation Fee Applicability (ccs_implementationfeeapplicability)"= text,
- #"Implementation Fee Applicability"= text,
- #"Implementation Fee Currency (ccs_implementationfeecurrency)"= text,
- #"Implementation Fee Currency"= text,
- #"Implementation Fee"= text,
- #"Implementation Fee USD"= text,
- #"Weightage %"= number,
- #"Weighted Avg"= number,
- #"Closed Won Date"= date,
- #"Company Name"= text,
- #"leadid"= text ],{})
- else #"Converted to Table",
- #"Converted to Table" = Table.FromList(ResultsList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
- #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1",
- {
- "ccs_gid",
- "createdon",
- "_ownerid_value",
- "_ccs_country_of_sale_value",
- "_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue",
- "ccs_lineofbusiness",
- "_ccs_account_value",
- "_ccs_products_value",
- "ccs_annualrecurringrevenue",
- "ccs_arrinusd",
- "ccs_targetmargin",
- "ccs_reportingstage",
- "ccs_closedstatus",
- "leadsourcecode",
- "ccs_headcount",
- "ccs_expected_closure_date",
- "ccs_target_go_live_quarter",
- "ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue",
- "ccs_target_go_live_period",
- "ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue",
- "_modifiedby_value",
- "modifiedon",
- "ccs_latest_update",
- "ccs_agreementclosuredate",
- "ccs_implementationfee",
- "ccs_implementationfeeapplicability",
- "ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue",
- "_ccs_implementationfeecurrency_value",
- "_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue",
- "ccs_implementation_fee",
- "ccs_implementationfeeusd",
- "ccs_weightage",
- "ccs_weightedaverage",
- "ccs_wondate",
- "companyname",
- "leadid"
- },
- {
- "ccs_gid",
- "createdon",
- "_ownerid_value",
- "_ccs_country_of_sale_value",
- "_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue",
- "ccs_lineofbusiness",
- "_ccs_account_value",
- "_ccs_products_value",
- "ccs_annualrecurringrevenue",
- "ccs_arrinusd",
- "ccs_targetmargin",
- "ccs_reportingstage",
- "ccs_closedstatus",
- "leadsourcecode",
- "ccs_headcount",
- "ccs_expected_closure_date",
- "ccs_target_go_live_quarter",
- "ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue",
- "ccs_target_go_live_period",
- "ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue",
- "_modifiedby_value",
- "modifiedon",
- "ccs_latest_update",
- "ccs_agreementclosuredate",
- "ccs_implementationfee",
- "ccs_implementationfeeapplicability",
- "ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue",
- "_ccs_implementationfeecurrency_value",
- "_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue",
- "ccs_implementation_fee",
- "ccs_implementationfeeusd",
- "ccs_weightage",
- "ccs_weightedaverage",
- "ccs_wondate",
- "companyname",
- "leadid"
- }),
- #"Renamed Columns" = Table.RenameColumns(#"Expanded Column1",
- {
- {"ccs_gid", "Lead ID"},
- {"createdon", "Created On"},
- {"_ownerid_value", "Owner (ownerid)"},
- {"[email protected]", "Owner (Type)"},
- {"[email protected]", "Owner"},
- {"_ccs_country_of_sale_value", "Country of sale (ccs_country_of_sale)"},
- {"_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue", "Country of sale"},
- {"ccs_lineofbusiness", "Line of Business (ccs_lineofbusiness)"},
- {"[email protected]", "Line of Business"},
- {"_ccs_account_value", "Account (ccs_account)"},
- {"[email protected]", "Account"},
- {"_ccs_products_value", "Products (ccs_products)"},
- {"[email protected]", "Products"},
- {"ccs_annualrecurringrevenue", "Annual Recurring Revenue"},
- {"ccs_arrinusd", "ARR in USD"},
- {"[email protected]", "ARR in USD(Formatted Value)"},
- {"ccs_targetmargin", "Target Margin% (ccs_targetmargin)"},
- {"[email protected]", "Target Margin%"},
- {"ccs_reportingstage", "Stage Name"},
- {"ccs_closedstatus", "Closed Status (ccs_closedstatus)"},
- {"[email protected]", "Closed Status"},
- {"leadsourcecode", "Lead Source (leadsourcecode)"},
- {"[email protected]", "Lead Source"},
- {"ccs_headcount", "Head Count"},
- {"ccs_expected_closure_date", "Expected Closure Date"},
- {"ccs_target_go_live_quarter", "Target Go Live Quarter (ccs_target_go_live_quarter)"},
- {"ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue", "Target Go Live Quarter"},
- {"ccs_target_go_live_period", "Target Go Live Period (ccs_target_go_live_period)"},
- {"ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue", "Target Go Live Period"},
- {"_modifiedby_value", "Modified By (modifiedby)"},
- {"[email protected]", "Modified By"},
- {"modifiedon", "Modified On"},
- {"ccs_latest_update", "Latest Update"},
- {"ccs_agreementclosuredate", "Agreement Closure Date"},
- {"ccs_implementationfee", "Implementation Fee Charged (ccs_implementationfee)"},
- {"[email protected]", "Implementation Fee Charged"},
- {"ccs_implementationfeeapplicability", "Implementation Fee Applicability (ccs_implementationfeeapplicability)"},
- {"ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue", "Implementation Fee Applicability"},
- {"_ccs_implementationfeecurrency_value", "Implementation Fee Currency (ccs_implementationfeecurrency)"},
- {"_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue", "Implementation Fee Currency"},
- {"ccs_implementation_fee", "Implementation Fee"},
- {"ccs_implementationfeeusd", "Implementation Fee USD"},
- {"ccs_weightage", "Weightage %"},
- {"ccs_weightedaverage", "Weighted Avg"},
- {"ccs_wondate", "Closed Won Date"},
- {"companyname", "Company Name"},
- {"leadid", "leadid"}
- }),
- #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",
- {
- {"Lead ID", type text},
- {"Created On", type datetime},
- {"Owner (ownerid)", type text},
- {"Owner (Type)", type text},
- {"Owner", type text},
- {"Country of sale (ccs_country_of_sale)", type text},
- {"Country of sale", type text},
- {"Line of Business (ccs_lineofbusiness)", type text},
- {"Line of Business", type text},
- {"Account (ccs_account)", type text},
- {"Account", type text},
- {"Products (ccs_products)", type text},
- {"Products", type text},
- {"Annual Recurring Revenue", type number},
- {"ARR in USD", type number},
- {"ARR in USD(Formatted Value)", type number},
- {"Target Margin% (ccs_targetmargin)", type text},
- {"Target Margin%", type text},
- {"Stage Name", type text},
- {"Closed Status (ccs_closedstatus)", type text},
- {"Closed Status", type text},
- {"Lead Source (leadsourcecode)", type text},
- {"Lead Source", type text},
- {"Head Count", type number},
- {"Expected Closure Date", type datetime},
- {"Target Go Live Quarter (ccs_target_go_live_quarter)", type text},
- {"Target Go Live Quarter", type text},
- {"Target Go Live Period (ccs_target_go_live_period)", type text},
- {"Target Go Live Period", type text},
- {"Modified By (modifiedby)", type text},
- {"Modified By", type text},
- {"Modified On", type datetime},
- {"Latest Update", type text},
- {"Agreement Closure Date", type datetime},
- {"Implementation Fee Charged (ccs_implementationfee)", type text},
- {"Implementation Fee Charged", type text},
- {"Implementation Fee Applicability (ccs_implementationfeeapplicability)", type text},
- {"Implementation Fee Applicability", type text},
- {"Implementation Fee Currency (ccs_implementationfeecurrency)", type text},
- {"Implementation Fee Currency", type text},
- {"Implementation Fee", type text},
- {"Implementation Fee USD", type text},
- {"Weightage %", type number},
- {"Weighted Avg", type number},
- {"Closed Won Date", type date},
- {"Company Name", type text},
- {"leadid", type text}
- })
- ,
- #"Added Link" = Table.AddColumn(#"Changed Type", "Link", each Dyn365CEBaseURL & "/main.aspx?etn=lead&pagetype=entityrecord&id=%7b"& [leadid]&"%7d"),
- #"Result" = if List.IsEmpty(ResultsList)
- then ResultsTable
- else #"Added Link",
- #"Removed Columns" = Table.RemoveColumns(Result,{"Owner (ownerid)", "Owner (Type)", "Country of sale (ccs_country_of_sale)", "Line of Business (ccs_lineofbusiness)", "Account (ccs_account)", "Products (ccs_products)", "Annual Recurring Revenue", "Target Margin% (ccs_targetmargin)", "ARR in USD(Formatted Value)", "Closed Status (ccs_closedstatus)", "Lead Source (leadsourcecode)", "Target Go Live Quarter (ccs_target_go_live_quarter)", "Target Go Live Period (ccs_target_go_live_period)", "Modified By (modifiedby)", "Implementation Fee Charged (ccs_implementationfee)", "Implementation Fee Applicability (ccs_implementationfeeapplicability)", "Implementation Fee Currency (ccs_implementationfeecurrency)", "Closed Won Date", "leadid"}),
- #"Added ARR Cluster" = Table.AddColumn(#"Removed Columns", "Arr Cluster", each if [ARR in USD] <= 100000 then "Silver" else if [ARR in USD] <= 200000 then "Gold" else if [ARR in USD] <= 300000 then "Platinum" else if [ARR in USD] > 300000 then "Diamond" else null),
- #"Replaced Errors(ARR Cluster)" = Table.ReplaceErrorValues(#"Added ARR Cluster", {{"Arr Cluster", "Silver"}}),
- #"Replaced null to Open(Closed Status)" = Table.ReplaceValue(#"Replaced Errors(ARR Cluster)",null,"Open",Replacer.ReplaceValue,{"Closed Status"}),
- #"Added ARR Cluster Sort" = Table.AddColumn(#"Replaced null to Open(Closed Status)", "ARRClusterSort", each if [Arr Cluster] = "Silver" then 1 else if [Arr Cluster] = "Gold" then 2 else if [Arr Cluster] = "Platinum" then 3 else 4),
- #"Filtered Rows" = Table.SelectRows(#"Added ARR Cluster Sort", each ([Stage Name] = "6-Closed Contracted") and ([Closed Status] = "Won")),
- #"Inserted ACD Quarter" = Table.AddColumn(#"Filtered Rows", "ACD Quarter", each Date.QuarterOfYear([Agreement Closure Date]), Int64.Type),
- #"Inserted ACD Month" = Table.AddColumn(#"Inserted ACD Quarter", "ACD Month", each Date.Month([Agreement Closure Date]), Int64.Type),
- #"Inserted ACD Year" = Table.AddColumn(#"Inserted ACD Month", "ACD Year", each Date.Year([Agreement Closure Date]), Int64.Type),
- #"Added ACD Quarter Name" = Table.AddColumn(#"Inserted ACD Year", "ACD Quarter Name", each if [ACD Quarter] = 1 then "Q1" else if [ACD Quarter] = 2 then "Q2" else if [ACD Quarter] = 3 then "Q3" else if [ACD Quarter] = 4 then "Q4" else null),
- #"Added ACD Month Name" = Table.AddColumn(#"Added ACD Quarter Name", "ACD Month Name", each if [ACD Month] = 1 then "January" else if [ACD Month] = 2 then "February" else if [ACD Month] = 3 then "March" else if [ACD Month] = 4 then "April" else if [ACD Month] = 5 then "May" else if [ACD Month] = 6 then "June" else if [ACD Month] = 7 then "July" else if [ACD Month] = 8 then "August" else if [ACD Month] = 9 then "September" else if [ACD Month] = 10 then "October" else if [ACD Month] = 11 then "November" else if [ACD Quarter] = 12 then "December" else null),
- #"Grouped Rows" = Table.Group(#"Added ACD Month Name", {"Country of sale", "Line of Business", "ACD Quarter Name", "ACD Month Name", "ACD Year"}, {{"ARR in USD", each List.Sum([ARR in USD]), type number}}),
- #"Added Month Range" = Table.AddColumn(#"Grouped Rows", "Month Range", each if [ACD Quarter Name] = "Q1" then "Jan - Mar" else if [ACD Quarter Name] = "Q2" then "Apr - Jun" else if [ACD Quarter Name] = "Q3" then "Jul - Sep" else if [ACD Quarter Name] = "Q4" then "Oct - Dec" else null),
- #"Uppercased Text" = Table.TransformColumns(#"Added Month Range",{{"Country of sale", Text.Upper, type text}, {"Line of Business", Text.Upper, type text}, {"ACD Month Name", Text.Upper, type text}, {"Month Range", Text.Upper, type text}}),
- #"Merged Queries" = Table.NestedJoin(#"Uppercased Text",{"Country of sale", "Line of Business", "ACD Month Name", "ACD Quarter Name", "ACD Year"},#"Target Data_new",{"Country", "Line of Business", "Month", "Quarter", "Year"},"Target Data_new",JoinKind.FullOuter),
- #"Expanded Target Data_new" = Table.ExpandTableColumn(#"Merged Queries", "Target Data_new", {"Country", "Line of Business", "Q-Year", "Month", "Quarter", "Year", "Month Ranges", "Target"}, {"Target Data_new.Country", "Target Data_new.Line of Business", "Target Data_new.Q-Year", "Target Data_new.Month", "Target Data_new.Quarter", "Target Data_new.Year", "Target Data_new.Month Ranges", "Target Data_new.Target"})
- in
- #"Expanded Target Data_new"
Advertisement
Add Comment
Please, Sign In to add comment