Guest User

Untitled

a guest
Oct 1st, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.26 KB | None | 0 0
  1. let
  2. GetResults = (z as text, x as number) =>
  3. let
  4. S = Json.Document(Web.Contents(ServiceRootURL & "/leads", [Headers=[Prefer="odata.include-annotations=*"],Query=[fetchXml="
  5. <fetch page=""" & Text.From(x) & """ paging-cookie=""" & z & """>
  6. <entity name=""lead""> <all-attributes />
  7.  
  8. </entity>
  9. </fetch>"]])),
  10. P = try Xml.Document(S[#"@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"]) otherwise null,
  11. R = if P <> null
  12. 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], ">", "&gt;"), "<", "&lt;"), """", "&quot;"), x + 1)})
  13. else S[value]
  14. in
  15. R,
  16. ResultsList = GetResults("", 1),
  17. ResultsTable = if List.IsEmpty(ResultsList)
  18. then #table(
  19. type table[ #"Lead ID"= text,
  20. #"Created On"= datetimezone,
  21. #"Owner (ownerid)"= text,
  22. #"Owner (Type)"= text,
  23. #"Owner"= text,
  24. #"Country of sale (ccs_country_of_sale)"= text,
  25. #"Country of sale"= text,
  26. #"Line of Business (ccs_lineofbusiness)"= text,
  27. #"Line of Business"= text,
  28. #"Account (ccs_account)"= text,
  29. #"Account"= text,
  30. #"Products (ccs_products)"= text,
  31. #"Products"= text,
  32. #"Annual Recurring Revenue"= number,
  33. #"ARR in USD"= text,
  34. #"ARR in USD(Formatted Value)"= text,
  35. #"Target Margin% (ccs_targetmargin)"= text,
  36. #"Target Margin%"= text,
  37. #"Stage Name"= text,
  38. #"Closed Status (ccs_closedstatus)"= text,
  39. #"Closed Status"= text,
  40. #"Lead Source (leadsourcecode)"= text,
  41. #"Lead Source"= text,
  42. #"Head Count"= number,
  43. #"Expected Closure Date"= date,
  44. #"Target Go Live Quarter (ccs_target_go_live_quarter)"= text,
  45. #"Target Go Live Quarter"= text,
  46. #"Target Go Live Period (ccs_target_go_live_period)"= text,
  47. #"Target Go Live Period"= text,
  48. #"Modified By (modifiedby)"= text,
  49. #"Modified By"= text,
  50. #"Modified On"= datetimezone,
  51. #"Latest Update"= text,
  52. #"Agreement Closure Date"= date,
  53. #"Implementation Fee Charged (ccs_implementationfee)"= text,
  54. #"Implementation Fee Charged"= text,
  55. #"Implementation Fee Applicability (ccs_implementationfeeapplicability)"= text,
  56. #"Implementation Fee Applicability"= text,
  57. #"Implementation Fee Currency (ccs_implementationfeecurrency)"= text,
  58. #"Implementation Fee Currency"= text,
  59. #"Implementation Fee"= text,
  60. #"Implementation Fee USD"= text,
  61. #"Weightage %"= number,
  62. #"Weighted Avg"= number,
  63. #"Closed Won Date"= date,
  64. #"Company Name"= text,
  65. #"leadid"= text ],{})
  66. else #"Converted to Table",
  67. #"Converted to Table" = Table.FromList(ResultsList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
  68. #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1",
  69. {
  70. "ccs_gid",
  71. "createdon",
  72. "_ownerid_value",
  73. "_ccs_country_of_sale_value",
  74. "_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue",
  75. "ccs_lineofbusiness",
  76. "_ccs_account_value",
  77. "_ccs_products_value",
  78. "ccs_annualrecurringrevenue",
  79. "ccs_arrinusd",
  80. "ccs_targetmargin",
  81. "ccs_reportingstage",
  82. "ccs_closedstatus",
  83. "leadsourcecode",
  84. "ccs_headcount",
  85. "ccs_expected_closure_date",
  86. "ccs_target_go_live_quarter",
  87. "ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue",
  88. "ccs_target_go_live_period",
  89. "ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue",
  90. "_modifiedby_value",
  91. "modifiedon",
  92. "ccs_latest_update",
  93. "ccs_agreementclosuredate",
  94. "ccs_implementationfee",
  95. "ccs_implementationfeeapplicability",
  96. "ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue",
  97. "_ccs_implementationfeecurrency_value",
  98. "_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue",
  99. "ccs_implementation_fee",
  100. "ccs_implementationfeeusd",
  101. "ccs_weightage",
  102. "ccs_weightedaverage",
  103. "ccs_wondate",
  104. "companyname",
  105. "leadid"
  106. },
  107.  
  108. {
  109. "ccs_gid",
  110. "createdon",
  111. "_ownerid_value",
  112. "_ccs_country_of_sale_value",
  113. "_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue",
  114. "ccs_lineofbusiness",
  115. "_ccs_account_value",
  116. "_ccs_products_value",
  117. "ccs_annualrecurringrevenue",
  118. "ccs_arrinusd",
  119. "ccs_targetmargin",
  120. "ccs_reportingstage",
  121. "ccs_closedstatus",
  122. "leadsourcecode",
  123. "ccs_headcount",
  124. "ccs_expected_closure_date",
  125. "ccs_target_go_live_quarter",
  126. "ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue",
  127. "ccs_target_go_live_period",
  128. "ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue",
  129. "_modifiedby_value",
  130. "modifiedon",
  131. "ccs_latest_update",
  132. "ccs_agreementclosuredate",
  133. "ccs_implementationfee",
  134. "ccs_implementationfeeapplicability",
  135. "ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue",
  136. "_ccs_implementationfeecurrency_value",
  137. "_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue",
  138. "ccs_implementation_fee",
  139. "ccs_implementationfeeusd",
  140. "ccs_weightage",
  141. "ccs_weightedaverage",
  142. "ccs_wondate",
  143. "companyname",
  144. "leadid"
  145. }),
  146. #"Renamed Columns" = Table.RenameColumns(#"Expanded Column1",
  147. {
  148. {"ccs_gid", "Lead ID"},
  149. {"createdon", "Created On"},
  150. {"_ownerid_value", "Owner (ownerid)"},
  151. {"[email protected]", "Owner (Type)"},
  152. {"[email protected]", "Owner"},
  153. {"_ccs_country_of_sale_value", "Country of sale (ccs_country_of_sale)"},
  154. {"_ccs_country_of_sale_value@OData.Community.Display.V1.FormattedValue", "Country of sale"},
  155. {"ccs_lineofbusiness", "Line of Business (ccs_lineofbusiness)"},
  156. {"[email protected]", "Line of Business"},
  157. {"_ccs_account_value", "Account (ccs_account)"},
  158. {"[email protected]", "Account"},
  159. {"_ccs_products_value", "Products (ccs_products)"},
  160. {"[email protected]", "Products"},
  161. {"ccs_annualrecurringrevenue", "Annual Recurring Revenue"},
  162. {"ccs_arrinusd", "ARR in USD"},
  163. {"[email protected]", "ARR in USD(Formatted Value)"},
  164. {"ccs_targetmargin", "Target Margin% (ccs_targetmargin)"},
  165. {"[email protected]", "Target Margin%"},
  166. {"ccs_reportingstage", "Stage Name"},
  167. {"ccs_closedstatus", "Closed Status (ccs_closedstatus)"},
  168. {"[email protected]", "Closed Status"},
  169. {"leadsourcecode", "Lead Source (leadsourcecode)"},
  170. {"[email protected]", "Lead Source"},
  171. {"ccs_headcount", "Head Count"},
  172. {"ccs_expected_closure_date", "Expected Closure Date"},
  173. {"ccs_target_go_live_quarter", "Target Go Live Quarter (ccs_target_go_live_quarter)"},
  174. {"ccs_target_go_live_quarter@OData.Community.Display.V1.FormattedValue", "Target Go Live Quarter"},
  175. {"ccs_target_go_live_period", "Target Go Live Period (ccs_target_go_live_period)"},
  176. {"ccs_target_go_live_period@OData.Community.Display.V1.FormattedValue", "Target Go Live Period"},
  177. {"_modifiedby_value", "Modified By (modifiedby)"},
  178. {"[email protected]", "Modified By"},
  179. {"modifiedon", "Modified On"},
  180. {"ccs_latest_update", "Latest Update"},
  181. {"ccs_agreementclosuredate", "Agreement Closure Date"},
  182. {"ccs_implementationfee", "Implementation Fee Charged (ccs_implementationfee)"},
  183. {"[email protected]", "Implementation Fee Charged"},
  184. {"ccs_implementationfeeapplicability", "Implementation Fee Applicability (ccs_implementationfeeapplicability)"},
  185. {"ccs_implementationfeeapplicability@OData.Community.Display.V1.FormattedValue", "Implementation Fee Applicability"},
  186. {"_ccs_implementationfeecurrency_value", "Implementation Fee Currency (ccs_implementationfeecurrency)"},
  187. {"_ccs_implementationfeecurrency_value@OData.Community.Display.V1.FormattedValue", "Implementation Fee Currency"},
  188. {"ccs_implementation_fee", "Implementation Fee"},
  189. {"ccs_implementationfeeusd", "Implementation Fee USD"},
  190. {"ccs_weightage", "Weightage %"},
  191. {"ccs_weightedaverage", "Weighted Avg"},
  192. {"ccs_wondate", "Closed Won Date"},
  193. {"companyname", "Company Name"},
  194. {"leadid", "leadid"}
  195. }),
  196. #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",
  197. {
  198. {"Lead ID", type text},
  199. {"Created On", type datetime},
  200. {"Owner (ownerid)", type text},
  201. {"Owner (Type)", type text},
  202. {"Owner", type text},
  203. {"Country of sale (ccs_country_of_sale)", type text},
  204. {"Country of sale", type text},
  205. {"Line of Business (ccs_lineofbusiness)", type text},
  206. {"Line of Business", type text},
  207. {"Account (ccs_account)", type text},
  208. {"Account", type text},
  209. {"Products (ccs_products)", type text},
  210. {"Products", type text},
  211. {"Annual Recurring Revenue", type number},
  212. {"ARR in USD", type number},
  213. {"ARR in USD(Formatted Value)", type number},
  214. {"Target Margin% (ccs_targetmargin)", type text},
  215. {"Target Margin%", type text},
  216. {"Stage Name", type text},
  217. {"Closed Status (ccs_closedstatus)", type text},
  218. {"Closed Status", type text},
  219. {"Lead Source (leadsourcecode)", type text},
  220. {"Lead Source", type text},
  221. {"Head Count", type number},
  222. {"Expected Closure Date", type datetime},
  223. {"Target Go Live Quarter (ccs_target_go_live_quarter)", type text},
  224. {"Target Go Live Quarter", type text},
  225. {"Target Go Live Period (ccs_target_go_live_period)", type text},
  226. {"Target Go Live Period", type text},
  227. {"Modified By (modifiedby)", type text},
  228. {"Modified By", type text},
  229. {"Modified On", type datetime},
  230. {"Latest Update", type text},
  231. {"Agreement Closure Date", type datetime},
  232. {"Implementation Fee Charged (ccs_implementationfee)", type text},
  233. {"Implementation Fee Charged", type text},
  234. {"Implementation Fee Applicability (ccs_implementationfeeapplicability)", type text},
  235. {"Implementation Fee Applicability", type text},
  236. {"Implementation Fee Currency (ccs_implementationfeecurrency)", type text},
  237. {"Implementation Fee Currency", type text},
  238. {"Implementation Fee", type text},
  239. {"Implementation Fee USD", type text},
  240. {"Weightage %", type number},
  241. {"Weighted Avg", type number},
  242. {"Closed Won Date", type date},
  243. {"Company Name", type text},
  244. {"leadid", type text}
  245. })
  246. ,
  247. #"Added Link" = Table.AddColumn(#"Changed Type", "Link", each Dyn365CEBaseURL & "/main.aspx?etn=lead&pagetype=entityrecord&id=%7b"& [leadid]&"%7d"),
  248. #"Result" = if List.IsEmpty(ResultsList)
  249. then ResultsTable
  250. else #"Added Link",
  251. #"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"}),
  252. #"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),
  253. #"Replaced Errors(ARR Cluster)" = Table.ReplaceErrorValues(#"Added ARR Cluster", {{"Arr Cluster", "Silver"}}),
  254. #"Replaced null to Open(Closed Status)" = Table.ReplaceValue(#"Replaced Errors(ARR Cluster)",null,"Open",Replacer.ReplaceValue,{"Closed Status"}),
  255. #"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),
  256. #"Filtered Rows" = Table.SelectRows(#"Added ARR Cluster Sort", each ([Stage Name] = "6-Closed Contracted") and ([Closed Status] = "Won")),
  257. #"Inserted ACD Quarter" = Table.AddColumn(#"Filtered Rows", "ACD Quarter", each Date.QuarterOfYear([Agreement Closure Date]), Int64.Type),
  258. #"Inserted ACD Month" = Table.AddColumn(#"Inserted ACD Quarter", "ACD Month", each Date.Month([Agreement Closure Date]), Int64.Type),
  259. #"Inserted ACD Year" = Table.AddColumn(#"Inserted ACD Month", "ACD Year", each Date.Year([Agreement Closure Date]), Int64.Type),
  260. #"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),
  261. #"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),
  262. #"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}}),
  263. #"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),
  264. #"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}}),
  265. #"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),
  266. #"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"})
  267. in
  268. #"Expanded Target Data_new"
Advertisement
Add Comment
Please, Sign In to add comment