Last week, Hubert Joly stepped down as CEO of Best Buy (NYSE:BBY) following an incredible seven-year run at the helm. When Joly took over in late 2012, Best Buy's revenue was falling, putting mounting pressure on its profitability. The company's adjusted operating profit plunged to $1.5 billion in fiscal 2013 from $2.3 billion a year earlier.

But against all odds, Joly managed to turn Best Buy around. His decision to respond aggressively to its market share losses and margin erosion by cutting prices and doubling down on investments in the business proved prescient. As a result, while the stock fell more than 40% in the four months after Joly was hired -- bottoming at less than $12 -- it has since rocketed higher, topping out above $80 last year.

BBY Chart

Best Buy stock performance, data by YCharts.

Over the past two years, new Macy's (NYSE:M) CEO Jeff Gennette has begun implementing a strategy very similar to the one Joly used. Despite some initial signs of success, Macy's stock remains in the doldrums, trading for just seven times the company's projected 2019 earnings. If Gennette's turnaround plan succeeds in any meaningful way, Macy's shareholders could experience Best Buy-like gains over the next few years.

How Best Buy saved itself

By the time Joly became CEO, many pundits were already counting Best Buy out. The trend of "showrooming" -- consumers checking out a product at a store and then buying it online at a lower price -- seemed destined to destroy the consumer electronics giant's business model.

Joly brought Best Buy back to relevance by slashing prices (and offering to match competitors' prices), improving customer service, and boosting e-commerce sales. All of these moves tended to further depress Best Buy's profitability. However, the company simultaneously implemented dramatic cost reductions in areas that are not customer-facing to fund these price cuts and growth investments.

At first, it seemed like cost reductions would only partiy offset the profit pressure caused by the price cuts. However, the company eventually stabilized its gross margin and returned to solid comparable-store sales growth.

The exterior of a Best Buy store

It took a few years for Best Buy's turnaround plan to fully pay off. Image source: Best Buy.

As a result, while Best Buy's adjusted operating margin fell from 4.6% in fiscal 2011 and 4.4% in fiscal 2012 to a low of 2.9% in fiscal 2014, it then began to bounce back. It has posted a 4.6% adjusted operating margin in each of its past two fiscal years. In essence, the company has gotten its profit margin back to the levels of eight years ago -- except that now, revenue is rising rather than falling, making this level of profitability (potentially) sustainable.

Macy's is making similar moves -- with some success

Since 2017, Macy's has been pursuing a turnaround of its own. Like Best Buy, Macy's has invested heavily in its e-commerce operations. In fact, it is the No. 10 e-commerce retailer in the U.S., according to eMarketer, with annual sales of $7.44 billion -- just behind Best Buy's $7.53 billion. Macy's e-commerce revenue has been rising at a double-digit rate for a decade, and mobile app revenue is growing even faster, surpassing $1 billion last year.

Additionally, Macy's is investing to improve the store experience. It is putting Macy's Backstage off-price sections inside many of its stores, capitalizing on consumers' interest in hunting for deals. Last year, it also upgraded 50 high-performing locations (which it calls the Growth50) with new technology, fixtures, and lighting; an expanded selection of big-ticket items; and the introduction of food and beverage offerings within the stores.

The exterior of a Macy's store

Macy's CEO Jeff Gennette has prioritized big investments in the company's stores. Image source: Macy's.

Both of these initiatives are succeeding. Stores with these enhancements are posting much better sales results than the company's traditional stores. As a result, the company will open another 50 Backstage off-price shops within its stores and expand the Growth50 model to 100 more stores during 2019.

Thanks to its growth investments, it has achieved comps growth for six consecutive quarters, reversing a streak of 11 consecutive declines. However, some of these initiatives have weighed on profitability.

Macy's is about to kick off a multiyear productivity-enhancement program to address this issue. The company hasn't shared specific financial goals yet, but management has indicated that Macy's has significant opportunities to boost gross margin and reduce its operating expenses.

A compelling investment opportunity, despite the risks

There's a danger that in looking for "the next Best Buy," minor similarities on the surface could mask deeper differences that would make it hard to mimic Best Buy's comeback. In this case, one big disadvantage for Macy's relative to Best Buy is that apparel sales have steadily declined as a percentage of total U.S. consumer spending for the past few decades. By contrast, Best Buy operates in a growing market for technology products. Furthermore, Best Buy has a much higher share of its market than Macy's does.

On the other hand, consumers arguably have a greater need to visit stores when buying clothing -- as it can be hard to evaluate fit and color online -- than for the consumer electronics products that Best Buy sells. That gives Macy's an advantage in keeping its stores relevant.

There's no guarantee that it will execute a successful comeback. Nevertheless, it's in much better shape than many investors seem to realize, with sales having returned to growth and cost cuts on the way. This makes Macy's stock look like an attractive investment at its current bargain valuation.