Power Automateで一意なデータを取り出す方法


たとえばこんなデータがあったとしましょう。(2回目)
image.png
ここから、モデル名だけのリストを作りたい場合、または容量の違いを見ずに、モデル名とカラーだけのリストを作りたい場合、どうしたらよいでしょうか? Power Automateで。

重複の排除

Power Appsだとあるんですよね。 Distinct関数が。これが指定した列で一意なリストを得ることができます。
Power Automateには、ありません。 Distinctみたいな関数はないです。
そんな重複の削除、リストの特定の列から一意な値だけを取得する方法を紹介します。

まずはいつものSelect

まずは必要な列(というか一意にしたい列)だけで構成されるリスト(配列)を作る必要があります。これにはSelectアクションを利用します。
image.png

出来た配列は、単純に最初のリストでCapacityを取り除いただけで、NameとColorは重複したままです。

一意が欲しいならunion()

リストから一意な一覧を得たいなら、union()を使いましょう!
image.png

重要なのは戻り値の説明にある「重複はありません」です。unionの結果は重複を許さないんです。
例えば、 [1,2,3,4]と[3,4,5]をunionすると、戻り値は[1,2,3,4,5]です。この性質を利用します。

Selectの結果は body('Select') です。これを踏まえて。

union(body('Select'),json('[]'))

これで完成です!
json('[]') は空の配列をjsonとして生成しています。これと重複を含む配列をunionすると、元の配列からも重複が取り除かれた結果が得られます!

[
  {
    "Name": "iPhone XR",
    "Color": "Red"
  },
  {
    "Name": "iPhone XR",
    "Color": "Black"
  },
  {
    "Name": "iPhone 11",
    "Color": "White"
  }
]

無事NameとColorで一意のリストができました!

おわり

簡単なアクションとちょっとだけの数式で一意なリストを生成することができました。
今回のFlowは以下からダウンロードできますので、試してみてください。
https://github.com/mofumofu-dance/PowerApps365/blob/master/Samples/DistinctArray.zip

※友人のFlow忍者ことJohn Liu が動画で説明してくれています!ぜひご覧ください。
https://www.youtube.com/watch?v=dNvxMMdWmjk

My First Blog Post

Be yourself; Everyone else is already taken.

— Oscar Wilde.

This is the first post on my new blog. I’m just getting this new blog going, so stay tuned for more. Subscribe below to get notified when I post new updates.

Introduce Yourself (Example Post)

This is an example post, originally published as part of Blogging University. Enroll in one of our ten programs, and start your blog right.

You’re going to publish a post today. Don’t worry about how your blog looks. Don’t worry if you haven’t given it a name yet, or you’re feeling overwhelmed. Just click the “New Post” button, and tell us why you’re here.

Why do this?

  • Because it gives new readers context. What are you about? Why should they read your blog?
  • Because it will help you focus you own ideas about your blog and what you’d like to do with it.

The post can be short or long, a personal intro to your life or a bloggy mission statement, a manifesto for the future or a simple outline of your the types of things you hope to publish.

To help you get started, here are a few questions:

  • Why are you blogging publicly, rather than keeping a personal journal?
  • What topics do you think you’ll write about?
  • Who would you love to connect with via your blog?
  • If you blog successfully throughout the next year, what would you hope to have accomplished?

You’re not locked into any of this; one of the wonderful things about blogs is how they constantly evolve as we learn, grow, and interact with one another — but it’s good to know where and why you started, and articulating your goals may just give you a few other post ideas.

Can’t think how to get started? Just write the first thing that pops into your head. Anne Lamott, author of a book on writing we love, says that you need to give yourself permission to write a “crappy first draft”. Anne makes a great point — just start writing, and worry about editing it later.

When you’re ready to publish, give your post three to five tags that describe your blog’s focus — writing, photography, fiction, parenting, food, cars, movies, sports, whatever. These tags will help others who care about your topics find you in the Reader. Make sure one of the tags is “zerotohero,” so other new bloggers can find you, too.