Skip to content

Instantly share code, notes, and snippets.

@IvanBond
Created February 28, 2019 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanBond/1c1342bf0a72a9f963dc7a71ffdad4e1 to your computer and use it in GitHub Desktop.
Save IvanBond/1c1342bf0a72a9f963dc7a71ffdad4e1 to your computer and use it in GitHub Desktop.
let
Delay = 1, // X seconds
GroupID = Text.From( GroupID ), // from parameter GroupID
url_base = "https://www.yammer.com/api/v1/users/in_group/" & GroupID & ".json",
Source = List.Generate(
()=> [
i = 2, // we get 1st page by using fGetUsersPage in arg1
url = url_base,
Page = fGetUsersPage(url_base), // 1st page
more = true,
last_page = false // to get last page
],
// arg2 = do while
each [last_page] = false and [i]<100, // hardcoded limit, increase for groups with more than 5000 members
// arg3 - iteration
each
[ i = [i] + 1,
Page = try Function.InvokeAfter(
()=>fGetUsersPage(url_base & "?page=" & Text.From([i])), #duration(0,0,0,Delay)
) otherwise null,
more = try Value.Metadata(Page)[MoreAvailable]? = true otherwise false,
url = url_base & "?page=" & Text.From([i]),
last_page = if not [more] then true else false // [more] is actually a reference to the result of previous iteration
],
// arg4 - output of each iteration
each [[i], [url], [Page], [more], [last_page]]
),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1",
{"i", "url", "Page", "more", "more2"},
{"i", "url", "Page", "more", "more2"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Column1",{"Page"}),
#"Expanded Page" = Table.ExpandTableColumn(#"Removed Other Columns", "Page",
{"id", "state", "full_name", "job_title", "mugshot_url", "web_url", "activated_at", "following", "followers", "email"},
{"id", "state", "full_name", "job_title", "mugshot_url", "web_url", "activated_at", "following", "followers", "email"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Page",{{"id", Int64.Type}, {"state", type text},
{"full_name", type text}, {"job_title", type text},
{"mugshot_url", type text}, {"web_url", type text},
{"email", type text} } ),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"activated_at", type datetimezone}}),
#"Extracted Date" = Table.TransformColumns(#"Changed Type1",{{"activated_at", DateTime.Date, type date}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Extracted Date",{{"following", Int64.Type}, {"followers", Int64.Type}}),
#"Inserted Age" = Table.AddColumn(#"Changed Type2", "Age", each Date.From(DateTime.LocalNow()) - [activated_at], type duration),
#"Inserted Total Years" = Table.AddColumn(#"Inserted Age", "Total Years", each Duration.TotalDays([Age]) / 365, type number),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Total Years",{"Age"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Total Years", "UserYears"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns","48x48","100x100",Replacer.ReplaceText,{"mugshot_url"})
in
#"Replaced Value"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment