7

I'm trying to get a subset of companies using EntityClass by revenue and industry but it won't let me combine the properties?

See below example, I can find Walmart via revenue or industry but not by both? Multiple conditions work when I use a numeric condition e.g TakeLargest[] on revenue and employees so I'm not sure what's going on

Any help greatly appreciated!

enter image description here

enter image description here

enter image description here

  • Welcome to the site! Please copy your code into the question, so users can evaluate it and test, rather than pasting a screenshot. – Jason B. Apr 14 '22 at 14:33

2 Answers2

5

You can adapt this to your requirements.

IntersectedEntityClass[
  EntityClass["Company", {"TotalRevenue" -> TakeLargest[100]}]
  , EntityClass["Company", {"Industry" -> "Discount Stores"}], 
  SameTestProperties -> "Name"
  ]["Name"]

{"Costco Wholesale", "Target", "Walmart"}

Syed
  • 52,495
  • 4
  • 30
  • 85
5

This is a well-known problem in general: filters like TakeLargest must usually be applied last, since one usually wants to limit the already filtered result set. The short syntax does not respect that order.

The simplest way to get the desired result is probably this:

EntityList @ EntityClass[
  EntityClass["Company", {"Industry" -> "Discount Stores"}],
  {"TotalRevenue" -> TakeLargest[1]}
]

(* {Entity["Company", "WalMartStores::zsp93"]} *)

Operations such as TakeLargest are essentially composite in nature (in this case, essentially it is Sort followed by Take). It is instructive to consider an alternative approach, which is unfortunately considerably slower (for built-in entities. For entities backed by a relational database, the speed should be the same as in the first approach), but can be used as a blueprint for more complex filtering criteria:

EntityList @ SortedEntityClass[
  FilteredEntityClass[
    "Company",
    EntityFunction[c, c["Industry"] === "Discount Stores"]
  ],
  "TotalRevenue" -> "Descending",
  1
]

(* {Entity["Company", "WalMartStores::zsp93"]} *)

Here one could, of course, also use the short form EntityClass["Company", {"Industry" -> "Discount Stores"}] instead of FilteredEntityClass, which I have used just for an illustration.

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420