Created
June 11, 2024 20:59
-
-
Save JinKwonJeon/a01973ae2bca6a3e247c487f0baec168 to your computer and use it in GitHub Desktop.
NaverDataLab_Trend 함수 (PowerQuery M)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (Keywords as text, optional startDate as text, optional endDate as text, optional ClientID as text, optional ClientSecret as text) => | |
| let | |
| // Step 1: 시작 날짜 및 종료 날짜 설정 (옵션 입력값이 없을 경우 기본값으로 설정) | |
| sDate = if (startDate is null) then Text.From(DateTime.Date(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][StartDate]{0})) else startDate, | |
| eDate = if (endDate is null) then Text.From(DateTime.Date(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][EndDate]{0})) else endDate, | |
| // Step 2: 클라이언트 ID 및 시크릿 설정 (옵션 입력값이 없을 경우 기본값으로 설정) | |
| cID = if (ClientID is null) then Text.From(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][ClientID]{0}) else ClientID, | |
| cSecret = if (ClientSecret is null) then Text.From(Excel.CurrentWorkbook(){[Name="tblNaver"]}[Content][ClientSecret]{0}) else ClientSecret, | |
| // Step 3: 네이버 데이터랩 검색 API 요청을 위한 URL 및 본문 생성 | |
| url = "https://openapi.naver.com/v1/datalab/search", | |
| body = "{ | |
| ""startDate"": """ & sDate & """, | |
| ""endDate"": """ & eDate & """, | |
| ""timeUnit"": ""date"", | |
| ""keywordGroups"": [ | |
| { | |
| ""groupName"": """ & Keywords & """, | |
| ""keywords"": [ | |
| """ & Keywords & """ | |
| ] | |
| } | |
| ] | |
| }", | |
| // Step 4: 요청 헤더 설정 | |
| headers = [ | |
| #"Content-Type"="application/json", | |
| #"X-Naver-Client-Id"= cID, | |
| #"X-Naver-Client-Secret"=cSecret | |
| ], | |
| // Step 5: API 요청 보내기 및 응답 데이터 처리 | |
| Source = Json.Document(Web.Contents(url, [Content=Text.ToBinary(body), Headers=headers, ManualStatusHandling={400, 404, 500}])), | |
| results = Source[results], | |
| // Step 6: 결과를 테이블 형식으로 변환 및 필요한 열 확장 | |
| ListToTable = Table.FromList(results, Splitter.SplitByNothing(), null, null, ExtraValues.Error), | |
| ColName = List.First(Table.ColumnNames(ListToTable)), | |
| FieldNames = Record.FieldNames(Record.Field(ListToTable{0}, ColName)), | |
| ExpandTable = Table.ExpandRecordColumn(ListToTable, ColName, FieldNames, FieldNames), | |
| ExpandData = Table.RemoveColumns(Table.ExpandRecordColumn(Table.ExpandListColumn(ExpandTable, "data"), "data", {"period", "ratio"}, {"period", "ratio"}),{"keywords"}), | |
| // Step 7: 데이터 타입 변환 및 요일 이름 추가 | |
| ChangeType = Table.TransformColumnTypes(ExpandData,{{"period", type date}, {"ratio", type number}, {"title", type text}}), | |
| NaverDataLab_Trend = Table.ReorderColumns(Table.AddColumn(ChangeType, "Day Name", each Text.Start(Date.DayOfWeekName([period]),1), type text),{"title", "period", "Day Name", "ratio"}) | |
| in | |
| NaverDataLab_Trend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment